Android-BLE低消費電力Bluetooth開発


Android Bluetooth開発
1.初期化
mBLEService = new BluetoothLeService(this); //   android       ,      
BLECommunicateUtils.setBLEService(mBLEService);
mBLEService.setOnServiceDiscoverListener(mOnServiceDiscover);
mBLEService.setOnDataAvailableListener(mOnDataAvailable);
mBLEService.setOnDisconnectListener(mOnDisconnectListener);
mBLEService.setOnConnectListener(mOnConnectListener);

2.スキャン取得装置
/** * @param enable (    ,true:    ,false:    ) * @return void * @throws * @Title: scanLeDevice * @Description: TODO(      ) */
private void scanLeDevice(final boolean enable) {
    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
                if (mBLEService != null) {
                    mBLEService.close();
                }
                Log.e(TAG, "      ,    ");
            }
        }, SCAN_PERIOD);
        /*         , mLeScanCallback      */
        mScanning = true;
        mBluetoothAdapter.startLeScan(mLeScanCallback);
        Log.e(TAG, "    ");
    } else {
        mScanning = false;
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
        Log.e(TAG, "    ");
    }
}

3.Bluetoothのコールバックをスキャンする
/** *                  ,    BluetoothDevice,    name MAC    **/
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

    @Override
    public void onLeScan(final BluetoothDevice device, final int rssi,
                         byte[] scanRecord) {

        if (device.getName() != null && device.getName().equals(BLUETOOTH_NAME)) {
            Log.e(TAG, "        ");
            if (mScanning) {
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
                mScanning = false;
            }
            mBLEService.connect(device);
        }
    }
};

4.Bluetoothを接続するコールバックBluetoothGattCallback
/** * connect a remoteDevice callback */
private BluetoothGattCallback mGattCallBack = new BluetoothGattCallback() {

    //    
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            mBluetoothGatt.discoverServices();
            if (mOnConnectListener != null) {
                mOnConnectListener.onConnect(mBluetoothGatt);
            }

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            // 2016/4/25 if disconnect ,trigger mOnDisconnectListener
            if (mOnDisconnectListener != null) {
                mOnDisconnectListener.onDisconnect(mBluetoothGatt);
            }
        }
    }

    //    
    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        if (status == BluetoothGatt.GATT_SUCCESS && mOnServiceDiscoverListener != null) {
            mOnServiceDiscoverListener.onServiceDiscover(gatt);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicRead(gatt, characteristic, status);
        if (status==BluetoothGatt.GATT_SUCCESS && mOnDataAvailableListener != null) {
            mOnDataAvailableListener.onCharacteristicRead(gatt, characteristic);
        }
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
        if (mOnDataAvailableListener != null) {
            mOnDataAvailableListener.onCharacteristicWrite(gatt,
                    characteristic);
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicChanged(gatt, characteristic);
        if (mOnDataAvailableListener != null) {
            mOnDataAvailableListener.onCharacteristicWrite(gatt,
                    characteristic);
            mOnDataAvailableListener.onCharacteristicRead(gatt,characteristic);
        }
    }

    /* *      * */
    @Override
    public void onDescriptorRead(BluetoothGatt gatt,
                                 BluetoothGattDescriptor descriptor, int status) {
        super.onDescriptorRead(gatt, descriptor, status);
    }

    /* *      * */
    @Override
    public void onDescriptorWrite(BluetoothGatt gatt,
                                  BluetoothGattDescriptor descriptor, int status) {
        super.onDescriptorWrite(gatt, descriptor, status);
    }

    @Override
    public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
        super.onReliableWriteCompleted(gatt, status);
    }

    @Override
    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        super.onReadRemoteRssi(gatt, rssi, status);
    }

    @Override
    public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
        super.onMtuChanged(gatt, mtu, status);
    }
};

5.onServicesDiscoveredのコールバックでコマンドを送信する
/** * get the supported characteristics , maybe need to change * * @param gattServices gattServices */
private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null) {
        return;
    }
    for (BluetoothGattService gattService : gattServices) {
        List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
        for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
            if (gattCharacteristic.getUuid().toString().equals(HEART_RATE_MEASUREMENT)) {

                //   Characteristic     ,             mOnDataAvailable.onCharacteristicWrite()
                mBLEService.setCharacteristicNotification(
                        gattCharacteristic, true);
            }
            gattCharacteristic_charA = gattCharacteristic;
        }
    }

    BLECommunicateUtils.sendData(gattCharacteristic_charA, StringUtils.
            hexStringToBytes("***************    *****************"));
}

5.Bluetoothデバイスから返されたデータを受信する
設定が必要
//   Characteristic     ,             mOnDataAvailable.onCharacteristicWrite()
mBLEService.setCharacteristicNotification(
        gattCharacteristic, true);

Bluetooth接続のonCharacteristicChangedコールバックで受信したデータを処理
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    Log.e(TAG, "     " + characteristic.toString());
    //    
    if (mBLEService != null) {
        mBLEService.close();
    }
    Log.e(TAG, "    ");
}

6.注意事項:
  • Bluetooth
  • を閉じる
      /** * close the BluetoothGatt */
        public void close() {
            if (mBluetoothGatt == null) {
                return;
            }
            mBluetoothGatt.close();
    // mBluetoothGatt = null;        null    mBluetoothGatt     ,             .
        }

    7.Demo参考:http://download.csdn.net/detail/github_34224676/9793523Github転送ゲート:https://github.com/IOXusu/Android-BLE-Demo