0,Android幫助文檔
www.pinnace.cn/bluetooth/tech/1940.shtml
此示例代碼中有錯:
在DeviceControlActivity類內(nèi):ExpandableListView.OnChildClickListener
中if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0)的|應(yīng)該改成&
1,AndroidManifest.xml
2,DeviceScanActivity界面如下:

onCreate->onCreateOptionsMenu->onResume(new mLeDeviceListAdapter),單擊掃描onOptionsItemSelected->scanLeDevice搜索到設(shè)備會回調(diào)LeScanCallback接口BluetoothAdapter.LeScanCallback,單擊搜索到的藍(lán)牙設(shè)備列表(用戶操作)onListItemClick(把搜到的藍(lán)牙設(shè)備的地址和名稱放入Intent中傳給下個界面DeviceControlActivity)--》3DeviceControlActivity
3,DeviceControlActivity界面如下:調(diào)用BluetoothLeService中的方法:

onCreate
先取出上個界面DeviceScanActivity傳過來的藍(lán)牙設(shè)備的地址和名稱并顯示在界面上,然后
(Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
//上面2句執(zhí)行--》異步調(diào)用(1)new 1個mServiceConnection接口的對象(2)執(zhí)行BluetoothLeService類里面的
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {BluetoothLeService getService() {return BluetoothLeService.this;}}//通過調(diào)用mBinder.getService()可以使用mBinder對象獲得BluetoothLeService
----》執(zhí)行BluetoothLeService類里面的
public IBinder onBind(Intent intent) {return mBinder}綁定成功
----》執(zhí)行DeviceControlActivity
的onServiceConnected方法onServiceConnected:(1)BluetoothLeServicem.initialize()獲取BluetoothManager和mBluetoothAdapter初始化成功再執(zhí)行(2)BluetoothLeService.connect(mDeviceAddress):第1次連接時通過mBluetoothAdapter獲得BluetoothDevice,通過BluetoothDevice獲得mBluetoothGatt,以后再連直接連就可以。連接狀態(tài)由mConnectionState初始值STATE_DISCONNECTED變?yōu)镾TATE_CONNECTING---》會執(zhí)行BluetoothLeService的回調(diào)函數(shù)BluetoothGattCallback。onConnectionStateChange中把連接狀態(tài)放到Intent中用廣播sendBroadcast(intent)的方式告知本界面中
異步調(diào)用就是你 喊 你朋友吃飯 ,你朋友說知道了 ,待會忙完去找你 ,你就去做別的了。同步調(diào)用就是你 喊 你朋友吃飯 ,你朋友在忙 ,你就一直在那等,等你朋友忙完了 ,你們一起去。
bindService是異步調(diào)用和Service進(jìn)行綁定, 如果綁定成功, 則會調(diào)用ServiceConnection的onServiceConnected。在這個方法中會向Activity中傳遞一個IBinder的實(shí)例,Acitity需要保存這個實(shí)例。在Service中需要創(chuàng)建一個實(shí)現(xiàn)IBinder的內(nèi)部類(這個內(nèi)部類不一定在Service中實(shí)現(xiàn),但必須在Service中創(chuàng)建它)。在OnBind()方法中需返回一個IBinder實(shí)例,不然onServiceConnected方法不會調(diào)用。
//已經(jīng)注冊了此廣播(onResume中registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());在的廣播接收器中根據(jù)狀態(tài)進(jìn)行更新界面
onResume: registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
注冊廣播:如果藍(lán)牙服務(wù)出現(xiàn)下列4種狀態(tài)BluetoothLeService.ACTION_GATT_CONNECTED,ACTION_GATT_DISCONNECTED,ACTION_GATT_SERVICES_DISCOVERED,ACTION_DATA_AVAILABLE
這個廣播private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver()
就通知我做下面的事情:連上了,更新ui上的文字(菜單欄中)
invalidateOptionsMenu();//Menu ItemonCreateOptionsMenu()只有在menu剛被創(chuàng)建時才會執(zhí)行,因此要想隨時動態(tài)改變OptionMenu就要實(shí)現(xiàn)onPrepareOptionsMenu()方法,該方法會傳給你Menu對象,供使用 Android2.3或更低的版本會在每次Menu打開的時候調(diào)用一次onPrepareOptionsMenu().? Android3.0及以上版本默認(rèn)menu是打開的,所以必須調(diào)用invalidateOptionsMenu()方法,然后系統(tǒng)將調(diào)用onPrepareOptionsMenu()執(zhí)行update操作。
mBluetoothLeService.connect(mDeviceAddress);第1次進(jìn)入此界面,重新進(jìn)入此界面,單擊菜單欄的菜單項(xiàng):Connect都會去連接此藍(lán)牙設(shè)備,所以要寫3個地方
4,BluetoothLeService
public class LocalBinder extends Binder {
BluetoothLeService getService() {
return BluetoothLeService.this;}}
private final IBinder mBinder = new LocalBinder();
onBind:return mBinder;通過此mBinder找到此BluetoothLeService
調(diào)用 bindService 會啟動 Service,Service.onBind如果返回null,不會連接上 Service,因此?ServiceConnection.onServiceConnected 不會被調(diào)用,但你任然需要使用 unbindService 函數(shù)斷開它,這樣 Service?才會停止。
Intent intent = new Intent(MainActivity.this,BindService.class),bindService(intent, conn, Context.BIND_AUTO_CREATE);->新建了BindService對象->新建了MyBinder對象->onBind()函數(shù)? -----傳遞MyBinder對象------->onServiceConnected()--> 通過傳遞的Binder對象獲取剛剛和Binder對象對應(yīng)的BindService 對象 -->調(diào)用Service中定義的方法。
5,手機(jī)藍(lán)牙狀態(tài)的改變
調(diào)用mBluetoothLeService.connect(mDeviceAddress);手機(jī)和發(fā)射器的狀態(tài)由未連接-->已連接,運(yùn)行BluetoothLeService的回調(diào)函數(shù)onConnectionStateChange:--->broadcastUpdate(ACTION_GATT_CONNECTED)-->sendBroadcast(new Intent(ACTION_GATT_CONNECTED));-->運(yùn)行DeviceControlActivity的BroadcastReceiver--->界面的菜單欄上顯示
關(guān)閉藍(lán)牙同上面流程
6,手機(jī)和發(fā)射器藍(lán)牙連接上以后就會發(fā)現(xiàn)Services,BluetoothGattCallback的方法onServicesDiscovered就會執(zhí)行,同上
public void? onServicesDiscovered (BluetoothGatt gatt, int status)
Callback invoked when the list of remote services, characteristics and descriptors for the remote device have been updated, ie new services have been discovered.
--》DeviceControlActivitydisplayGattServices中(mBluetoothLeService.getSupportedGattServices());
參數(shù):BluetoothLeService中的方法getSupportedGattServices()獲得BluetoothGatt
支持的所有Service
此方法可以獲取該發(fā)射器支持的所有Service和每個Service下面的Characteristic如下圖:

7,單擊上圖中的Characteristic UUID--->執(zhí)行DeviceControlActivity:ExpandableListView.OnChildClickListener:
8,characteristic讀的流程
BluetoothGatt.
readCharacteristic(BluetoothGattCharacteristic characteristic)-->異步調(diào)用BluetoothGattCallback.onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)

service:1800,1801,180a,fff0
(1)service:1800--》1800指00001800-0000-1000-8000-00805f9b34fb,下面一樣
characteristic 2a00數(shù)據(jù):CGM Sensor
?????????????????????????????????????? 43? 47? 4D? 20? 53? 65? 6E? 73? 6F? 72
characteristic 2a01數(shù)據(jù):00? 00
characteristic 2a02數(shù)據(jù):00
characteristic 2a03數(shù)據(jù):00
characteristic 2a04數(shù)據(jù):P???
?????????????????????????????????????? 50 00 A0 00 00 00 E8 03
(2)service:1801--》
characteristic 2a05數(shù)據(jù):No data
(3)service:180a--》Device informaton Service
characteristic 2a23數(shù)據(jù):7o<??
???????????????????????????????????????37? 6F? 3C? 00? 00? 98? 80? 00
characteristic 2a24數(shù)據(jù):Model Number
????????????????????????????????????? 4D 6F? 64? 65? 6C? 20? 4E? 75? 6D? 62? 65? 72
characteristic 2a25數(shù)據(jù):Serial ?Number
53?65??72? 69? 61??6C??20??4E??75? 6D? 62? 65? 72
characteristic 2a26數(shù)據(jù):Firmware Revision
46?69? 72? 6D? 77? 61??72? 65? 20??52? 65? 76? 69??73? 69? 6F 6E
characteristic 2a27數(shù)據(jù):Hardware ?Revision
48 61? 72? 64? 77? 61? 72? 65? 20? 52? 65? 76? 69? 73? 69? 6F 6E
characteristic 2a28數(shù)據(jù):Software? Revision
53?6F? 66? 74? 77? 61? 72? 65? 20? 52? 65? 76? 69? 73? 69? 6F 6E
Manufacture Name String 2a29數(shù)據(jù):Manufacture Name
4D? 61? 6E? 75? 66? 61? 63? 74? 75? 72? 65? 72? 20? 4E? 61? 6D? 65
characteristic 2a2a數(shù)據(jù):?expriamental
FE 00? 65? 78? 70? 65? 72? 69? 6D? 65? 6E? 74? 61? 6C
characteristic 2a50數(shù)據(jù):
01? 0D? 00? 00? 00? 10? 01
(4)service:fff0--》
characteristic fff1數(shù)據(jù):No data
characteristic fff2數(shù)據(jù):No data
9,characteristic寫的流程
同上
10,android4.3.1連接總是會斷掉,是由于發(fā)射器的配置導(dǎo)致的