List of usage examples for android.bluetooth BluetoothGattCharacteristic WRITE_TYPE_NO_RESPONSE
int WRITE_TYPE_NO_RESPONSE
To view the source code for android.bluetooth BluetoothGattCharacteristic WRITE_TYPE_NO_RESPONSE.
Click Source Link
From source file:com.megster.cordova.rfduino.RFduinoPlugin.java
private void disconnect(CallbackContext callbackContext) { if (activePeripheral != null) { BluetoothGattCharacteristic characteristic = activePeripheral.getDisconnectCharacteristic(); characteristic.setValue(""); characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); gatt.writeCharacteristic(characteristic); activePeripheral.disconnect();// w w w . j av a2 s.co m } if (gatt != null) { gatt.close(); gatt = null; } callbackContext.success(); }
From source file:com.megster.cordova.rfduino.RFduinoPlugin.java
private void write(CallbackContext callbackContext, byte[] data) { BluetoothGattCharacteristic characteristic = activePeripheral.getSendCharacteristic(); characteristic.setValue(data);// ww w .j ava 2 s . com characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); boolean success = gatt.writeCharacteristic(characteristic); if (success) { callbackContext.success(); } else { callbackContext.error("Write Failed"); } }
From source file:com.megster.cordova.ble.central.BLECentralPlugin.java
@Override public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException { LOG.d(TAG, "action = " + action); if (bluetoothAdapter == null) { Activity activity = cordova.getActivity(); BluetoothManager bluetoothManager = (BluetoothManager) activity .getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); }/*from w w w . jav a 2s.co m*/ boolean validAction = true; if (action.equals(SCAN)) { UUID[] serviceUUIDs = parseServiceUUIDList(args.getJSONArray(0)); int scanSeconds = args.getInt(1); findLowEnergyDevices(callbackContext, serviceUUIDs, scanSeconds); } else if (action.equals(START_SCAN)) { UUID[] serviceUUIDs = parseServiceUUIDList(args.getJSONArray(0)); findLowEnergyDevices(callbackContext, serviceUUIDs, -1); } else if (action.equals(STOP_SCAN)) { bluetoothAdapter.stopLeScan(this); callbackContext.success(); } else if (action.equals(LIST)) { listKnownDevices(callbackContext); } else if (action.equals(CONNECT)) { String macAddress = args.getString(0); connect(callbackContext, macAddress); } else if (action.equals(DISCONNECT)) { String macAddress = args.getString(0); disconnect(callbackContext, macAddress); } else if (action.equals(READ)) { String macAddress = args.getString(0); UUID serviceUUID = uuidFromString(args.getString(1)); UUID characteristicUUID = uuidFromString(args.getString(2)); read(callbackContext, macAddress, serviceUUID, characteristicUUID); } else if (action.equals(WRITE)) { String macAddress = args.getString(0); UUID serviceUUID = uuidFromString(args.getString(1)); UUID characteristicUUID = uuidFromString(args.getString(2)); byte[] data = args.getArrayBuffer(3); int type = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT; write(callbackContext, macAddress, serviceUUID, characteristicUUID, data, type); } else if (action.equals(WRITE_WITHOUT_RESPONSE)) { String macAddress = args.getString(0); UUID serviceUUID = uuidFromString(args.getString(1)); UUID characteristicUUID = uuidFromString(args.getString(2)); byte[] data = args.getArrayBuffer(3); int type = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE; write(callbackContext, macAddress, serviceUUID, characteristicUUID, data, type); } else if (action.equals(START_NOTIFICATION)) { String macAddress = args.getString(0); UUID serviceUUID = uuidFromString(args.getString(1)); UUID characteristicUUID = uuidFromString(args.getString(2)); registerNotifyCallback(callbackContext, macAddress, serviceUUID, characteristicUUID); } else if (action.equals(STOP_NOTIFICATION)) { String macAddress = args.getString(0); UUID serviceUUID = uuidFromString(args.getString(1)); UUID characteristicUUID = uuidFromString(args.getString(2)); removeNotifyCallback(callbackContext, macAddress, serviceUUID, characteristicUUID); } else if (action.equals(IS_ENABLED)) { if (bluetoothAdapter.isEnabled()) { callbackContext.success(); } else { callbackContext.error("Bluetooth is disabled."); } } else if (action.equals(IS_CONNECTED)) { String macAddress = args.getString(0); if (peripherals.containsKey(macAddress) && peripherals.get(macAddress).isConnected()) { callbackContext.success(); } else { callbackContext.error("Not connected."); } } else if (action.equals(SETTINGS)) { Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); cordova.getActivity().startActivity(intent); callbackContext.success(); } else if (action.equals(ENABLE)) { enableBluetoothCallback = callbackContext; Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); cordova.startActivityForResult(this, intent, REQUEST_ENABLE_BLUETOOTH); } else { validAction = false; } return validAction; }
From source file:com.example.bluetooth.RFduinoService.java
public boolean send(byte[] data) { if (mBluetoothGatt == null || mBluetoothGattService == null) { Log.w(TAG, "BluetoothGatt not initialized"); return false; }/* w ww . j a v a 2 s. co m*/ BluetoothGattCharacteristic characteristic = mBluetoothGattService.getCharacteristic(UUID_SEND); if (characteristic == null) { Log.w(TAG, "Send characteristic not found"); return false; } characteristic.setValue(data); characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); return mBluetoothGatt.writeCharacteristic(characteristic); }
From source file:com.example.bluetooth.RFduinoService.java
public boolean disconnectRFduino() { if (mBluetoothGatt == null || mBluetoothGattService == null) { Log.w(TAG, "BluetoothGatt not initialized"); return false; }/*from w w w .j ava2 s . c o m*/ BluetoothGattCharacteristic characteristic = mBluetoothGattService.getCharacteristic(UUID_DISCONNECT); if (characteristic == null) { Log.w(TAG, "Disconnect characteristic not found"); return false; } characteristic.setValue(""); characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); return mBluetoothGatt.writeCharacteristic(characteristic); }
From source file:com.megster.cordova.ble.central.Peripheral.java
private BluetoothGattCharacteristic findWritableCharacteristic(BluetoothGattService service, UUID characteristicUUID, int writeType) { BluetoothGattCharacteristic characteristic = null; // get write property int writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE; if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) { writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE; }// w w w . j a v a 2 s. c om List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics(); for (BluetoothGattCharacteristic c : characteristics) { if ((c.getProperties() & writeProperty) != 0 && characteristicUUID.equals(c.getUuid())) { characteristic = c; break; } } // As a last resort, try and find ANY characteristic with this UUID, even if it doesn't have the correct properties if (characteristic == null) { characteristic = service.getCharacteristic(characteristicUUID); } return characteristic; }
From source file:com.megster.cordova.ble.central.Peripheral.java
private void processCommands() { LOG.d(TAG, "Processing Commands"); if (bleProcessing) { return;//from w ww .ja v a2 s .c o m } BLECommand command = commandQueue.poll(); if (command != null) { if (command.getType() == BLECommand.READ) { LOG.d(TAG, "Read " + command.getCharacteristicUUID()); bleProcessing = true; readCharacteristic(command.getCallbackContext(), command.getServiceUUID(), command.getCharacteristicUUID()); } else if (command.getType() == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT) { LOG.d(TAG, "Write " + command.getCharacteristicUUID()); bleProcessing = true; writeCharacteristic(command.getCallbackContext(), command.getServiceUUID(), command.getCharacteristicUUID(), command.getData(), command.getType()); } else if (command.getType() == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) { LOG.d(TAG, "Write No Response " + command.getCharacteristicUUID()); bleProcessing = true; writeCharacteristic(command.getCallbackContext(), command.getServiceUUID(), command.getCharacteristicUUID(), command.getData(), command.getType()); } else if (command.getType() == BLECommand.REGISTER_NOTIFY) { LOG.d(TAG, "Register Notify " + command.getCharacteristicUUID()); bleProcessing = true; registerNotifyCallback(command.getCallbackContext(), command.getServiceUUID(), command.getCharacteristicUUID()); } else if (command.getType() == BLECommand.REMOVE_NOTIFY) { LOG.d(TAG, "Remove Notify " + command.getCharacteristicUUID()); bleProcessing = true; removeNotifyCallback(command.getCallbackContext(), command.getServiceUUID(), command.getCharacteristicUUID()); } else { // this shouldn't happen throw new RuntimeException("Unexpected BLE Command type " + command.getType()); } } else { LOG.d(TAG, "Command Queue is empty."); } }
From source file:no.nordicsemi.android.nrftoolbox.dfu.DfuService.java
/** * Writes the image size to the characteristic. This method is SYNCHRONOUS and wait until the {@link BluetoothGattCallback#onCharacteristicWrite(BluetoothGatt, BluetoothGattCharacteristic, int)} * will be called or the connection state will change from {@link #STATE_CONNECTED_AND_READY}. If connection state will change, or an error will occur, an exception will be thrown. * //from w w w . j a v a 2 s .c om * @param gatt * the GATT device * @param characteristic * the characteristic to write to. Should be the DFU PACKET * @param imageSize * the image size in bytes * @throws DeviceDisconnectedException * @throws DfuException * @throws UploadAbortedException */ private void writeImageSize(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final int imageSize) throws DeviceDisconnectedException, DfuException, UploadAbortedException { mReceivedData = null; mErrorState = 0; mImageSizeSent = false; characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); characteristic.setValue(imageSize, BluetoothGattCharacteristic.FORMAT_UINT32, 0); gatt.writeCharacteristic(characteristic); // We have to wait for confirmation try { synchronized (mLock) { while ((mImageSizeSent == false && mConnectionState == STATE_CONNECTED_AND_READY && mErrorState == 0 && !mAborted) || mPaused) mLock.wait(); } } catch (final InterruptedException e) { loge("Sleeping interrupted", e); } if (mAborted) throw new UploadAbortedException(); if (mErrorState != 0) throw new DfuException("Unable to write Image Size", mErrorState); if (mConnectionState != STATE_CONNECTED_AND_READY) throw new DeviceDisconnectedException("Unable to write Image Size", mConnectionState); }