2015年12月21日 星期一

Android-防丟器為例













在還沒開始前,想必大家對 Bluetooth LE Scanner 不陌生吧
在開始編寫前可以先拿上方的應用程式操作看看




檢查手機版本和有無開啟藍芽
/** 初始化 */
public boolean initialize() {
    // For API level 18 and above, get a reference to BluetoothAdapter through
    // BluetoothManager.
    if (bluetoothManager == null) {
        bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (bluetoothManager == null) {
            Log.e(TAG, "Unable to initialize BluetoothManager.");
            return false;
        }
    }
    bluetoothAdapter = bluetoothManager.getAdapter();
    if (bluetoothAdapter == null) {
        Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
        return false;
    }
    return true;
}

裝置連線
/** 連線至藍芽裝置 */
public boolean connect(final String address) {
    if (bluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }
    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
            && bluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (bluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }
    final BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        return false;
    }
    bluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}

與裝置離線
public void disconnect() {
    if (bluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    bluetoothGatt.disconnect();
}

關閉藍芽
public void close() {
    if (bluetoothGatt == null) {
        return;
    }
    Log.w(TAG, "mBluetoothGatt closed");
    mBluetoothDeviceAddress = null;
    bluetoothGatt.close();
    bluetoothGatt = null;
}

連線後的回調
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.i(TAG, "Connected to GATT server.");
            Log.i(TAG, "Attempting to start service discovery:" +
                bluetoothGatt.discoverServices());
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            mConnectionState = STATE_DISCONNECTED;
            Log.i(TAG, "Disconnected from GATT server.");
            disconnect();
            close();
        }
        broadcastUpdate(intentAction);
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
                enableNotification();
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
            BluetoothGattCharacteristic characteristic,
            int status) {
        Log.w(TAG, "onCharacteristicRead received: " + characteristic.getValue());
        
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
            BluetoothGattCharacteristic characteristic) {
        Log.w(TAG, "onCharacteristicChanged received: " + characteristic.getValue());
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
        Log.w(TAG, "onCharacteristicWrite received: ");
    }
};

新增服務提醒
public void enableNotification()
{
    // is support SERVICE 
    BluetoothGattService Service = bluetoothGatt.getService(SERVICE_UUID);
    if (Service == null) {
        Log.w(TAG, "service not found!");
        return;
    }
    // is support Characteristic
    BluetoothGattCharacteristic Char = Service.getCharacteristic(CHARACTERISTIC_UUID);
    if (Char == null) {
        Log.w(TAG, "Tx charateristic not found!");
        return;
    }
    bluetoothGatt.setCharacteristicNotification(Char,true);
    BluetoothGattDescriptor descriptor = Char.getDescriptor(CCCD);
    final byte[] ENABLE_NOTIFICATION_VALUE__ = {0x01};
    descriptor.setValue(ENABLE_NOTIFICATION_VALUE__);
    bluetoothGatt.writeDescriptor(descriptor);
}

取得所有服務
for(BluetoothGattService bluetoothGattService: gatt.getServices())
{
    Log.w(TAG, "----------------------------------");
    Log.w(TAG, "BluetoothGattService : " + bluetoothGattService.getUuid() );
    for(BluetoothGattCharacteristic bluetoothGattCharacteristic: bluetoothGattService.getCharacteristics())
    {
        Log.w(TAG, "BluetoothGattCharacteristic : " + bluetoothGattCharacteristic.getUuid() );
        for(BluetoothGattDescriptor bluetoothGattDescriptro: bluetoothGattCharacteristic.getDescriptors())
        {
            Log.w(TAG, "BluetoothGattDescriptor : " + bluetoothGattDescriptro.getUuid());
            if(bluetoothGattDescriptro.getValue() != null)
                Log.w(TAG, "BluetoothGattDescriptor : " + bluetoothGattDescriptro.getValue());                
            
        }
    }
}

參考資料 : AndroidとBLE
參考資料 : NordicSemiconductor/Android-nRF-Toolbox
參考資料 : BLE Health Devices: First Steps with Android
參考資料 : Bluetooth Low Energy (or LE) is a very cool technology.
參考資料 : SensorTag User Guide_Android

沒有留言:

張貼留言