Android BLE開發(fā)入門到進(jìn)階(二)

通信流程

1.主設(shè)備APP掃描,搜索外圍智能設(shè)備
2.建立BLE藍(lán)牙連接
3.基于BLE藍(lán)牙連接 進(jìn)行數(shù)據(jù)通信

第一步 加權(quán)限,如果是6.0以上就自己手動去申請權(quán)限
主設(shè)備Android4.3+(API Level>=18)
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
第二步 獲得藍(lán)牙adapter
BluetoothManager bluetoothManager = (BluetoothManager)this.getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter mBtAdapter = bluetoothManager.getAdapter();

第三步 開始掃描
// 10秒后停止查找搜索.
private static final long SCAN_PERIOD = 10000;
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mBtAdapter .stopLeScan(DeviceScanActivity.this.mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mBtAdapter .startLeScan(DeviceScanActivity.this.mLeScanCallback);
掃描的結(jié)果通過這個mLeScanCallback接口回調(diào)返回

// 搜索到設(shè)備信息更新到主線程來更新UI界面
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};

第三步 連接(在實際項目中開發(fā),藍(lán)牙各種操作都是放在service中由于篇幅有限就簡寫了)
BluetoothGatt mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
連接狀態(tài)通過接口回調(diào)
// GATT返回值,例如連接狀態(tài)和service的改變 etc
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
//連接狀態(tài)改變
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        if (newState == BluetoothProfile.STATE_CONNECTED) {
                //已連接

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                //未連接
        }
    }

    //發(fā)現(xiàn)服務(wù)
    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
        //得到該智能設(shè)備所有service循環(huán)遍歷可以得到所有charactertictis
           gatt.getServices();
    }

    //被讀
    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
           // number += 1;
        }
    }

    //特性改變
    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
 
    }

    //特性書寫
    @Override
    public void onCharacteristicWrite (BluetoothGatt gatt,
                                       BluetoothGattCharacteristic characteristic,
                                       int status){
    
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容