Chinaunix首页 | 论坛 | 博客
  • 博客访问: 323021
  • 博文数量: 41
  • 博客积分: 2540
  • 博客等级: 少校
  • 技术积分: 570
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-18 11:29
文章分类

全部博文(41)

文章存档

2011年(4)

2010年(32)

2009年(3)

2008年(2)

我的朋友

分类: LINUX

2011-03-29 17:23:15

更改蓝牙名称失败,再次更改一次名称后显示为上一次的名称

原因为: BluetoothEventLoop中的onPropertyChanged函数在收到蓝牙名称改变的事件后,会先构造一个ACTION_LOCAL_NAME_CHANGED的Intent消息,然后再蓝牙名称存入HashMap Property中。 当BluetoothSetting收到了ACTION_LOCAL_NAME_CHANGED的Intent后就会调用getName方法从HashMap中读取蓝牙名称(BluetoothSetting里的操作和BluetoothEventLoop里的操作是异步进行的)。  但是如果此时CPU的占用过高或是本身硬件性能不够,会导致BluetootSetting读取到的是之前的蓝牙名称。
解决方法:
先向HashMap中写入蓝牙名称,然后再构造ACTION_LOCAL_NAME_CHANGED的Intent并发送。


/*package*/ void onPropertyChanged(String[] propValues) {
         if (mBluetoothService.isAdapterPropertiesEmpty()) {
             // We have got a property change before
             // we filled up our cache.
             mBluetoothService.getAllProperties();
         }
         String name = propValues[0];
         if (name.equals("Name")) {
             mBluetoothService.setProperty(name, propValues[1]);
             Intent intent = new Intent(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
             intent.putExtra(BluetoothAdapter.EXTRA_LOCAL_NAME, propValues[1]);
             mContext.sendBroadcast(intent, BLUETOOTH_PERM);


阅读(6712) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

hachiwolf2013-03-18 14:33:52

你好, 请问你是怎么解决这问题的!
这才是重点