Java Code Examples for android.bluetooth.BluetoothGattCallback#onConnectionStateChange()
The following examples show how to use
android.bluetooth.BluetoothGattCallback#onConnectionStateChange() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 6 votes |
@Test public void queueTestRetryCommand() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0); BluetoothGattCharacteristic characteristic2 = new BluetoothGattCharacteristic(UUID.fromString("00002A1E-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0); service.addCharacteristic(characteristic); service.addCharacteristic(characteristic2); when(gatt.readCharacteristic(characteristic)).thenReturn(true); when(gatt.getServices()).thenReturn(Arrays.asList(service)); peripheral.readCharacteristic(characteristic); verify(gatt).readCharacteristic(characteristic); // Trigger bonding to start callback.onCharacteristicRead(gatt, characteristic, 5); Field field = BluetoothPeripheral.class.getDeclaredField("bondStateReceiver"); field.setAccessible(true); BroadcastReceiver broadcastReceiver = (BroadcastReceiver) field.get(peripheral); Intent intent = mock(Intent.class); when(intent.getAction()).thenReturn(BluetoothDevice.ACTION_BOND_STATE_CHANGED); when(intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR)).thenReturn(BOND_BONDED); when(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)).thenReturn(device); broadcastReceiver.onReceive(context, intent); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); verify(gatt, times(2)).readCharacteristic(characteristic); }
Example 2
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 6 votes |
@Test public void setNotifyIndicationTest() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_INDICATE,0); BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"),0); service.addCharacteristic(characteristic); characteristic.addDescriptor(descriptor); when(gatt.getServices()).thenReturn(Arrays.asList(service)); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); peripheral.setNotify(characteristic, true); verify(gatt).setCharacteristicNotification(characteristic, true); assertEquals(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE[0], descriptor.getValue()[0]); assertEquals(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE[1], descriptor.getValue()[1]); verify(gatt).writeDescriptor(descriptor); callback.onDescriptorWrite(gatt, descriptor, 0); verify(peripheralCallback).onNotificationStateUpdate(peripheral, characteristic, 0); }
Example 3
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 6 votes |
@Test public void setNotifyNotificationTest() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_NOTIFY,0); BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"),0); service.addCharacteristic(characteristic); characteristic.addDescriptor(descriptor); when(gatt.getServices()).thenReturn(Arrays.asList(service)); peripheral.setNotify(characteristic, true); verify(gatt).setCharacteristicNotification(characteristic, true); assertEquals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE[0], descriptor.getValue()[0]); assertEquals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE[1], descriptor.getValue()[1]); verify(gatt).writeDescriptor(descriptor); callback.onDescriptorWrite(gatt, descriptor, 0); verify(peripheralCallback).onNotificationStateUpdate(peripheral, characteristic, 0); }
Example 4
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 6 votes |
@Test public void setNotifyDisableTest() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_NOTIFY,0); BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"),0); service.addCharacteristic(characteristic); characteristic.addDescriptor(descriptor); when(gatt.getServices()).thenReturn(Arrays.asList(service)); peripheral.setNotify(characteristic, false); verify(gatt).setCharacteristicNotification(characteristic, false); assertEquals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE[0], descriptor.getValue()[0]); assertEquals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE[1], descriptor.getValue()[1]); verify(gatt).writeDescriptor(descriptor); callback.onDescriptorWrite(gatt, descriptor, 0); verify(peripheralCallback).onNotificationStateUpdate(peripheral, characteristic, 0); }
Example 5
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 6 votes |
@Test public void queueTestConsecutiveReadsNoResponse() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0); BluetoothGattCharacteristic characteristic2 = new BluetoothGattCharacteristic(UUID.fromString("00002A1E-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0); service.addCharacteristic(characteristic); service.addCharacteristic(characteristic2); byte[] byteArray = new byte[] {0x01, 0x02, 0x03}; characteristic.setValue(byteArray); characteristic2.setValue(byteArray); when(gatt.readCharacteristic(characteristic)).thenReturn(true); peripheral.readCharacteristic(characteristic); peripheral.readCharacteristic(characteristic2); verify(gatt).readCharacteristic(characteristic); verify(peripheralCallback, never()).onCharacteristicUpdate(peripheral, byteArray, characteristic, 0); verify(gatt, never()).readCharacteristic(characteristic2); }
Example 6
Source File: Device.java From neatle with MIT License | 5 votes |
private void connectionFailed(int status) { BluetoothGattCallback current; int oldState; int newState; LinkedList<BluetoothGattCallback> queueCopy; BluetoothGatt oldGatt; synchronized (lock) { oldState = state; state = BluetoothGatt.STATE_DISCONNECTED; newState = state; serviceDiscovered = false; current = currentCallback; queueCopy = new LinkedList<>(queue); oldGatt = this.gatt; this.gatt = null; } NeatleLogger.i("Connection attempt failed. Notifying all pending operations"); current.onConnectionStateChange(oldGatt, status, BluetoothGatt.STATE_DISCONNECTED); for (BluetoothGattCallback cb : queueCopy) { cb.onConnectionStateChange(oldGatt, status, BluetoothGatt.STATE_DISCONNECTED); } notifyConnectionStateChange(oldState, newState); }
Example 7
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 5 votes |
@Test public void onConnectionStateChangedDisconnectedTest() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, BluetoothProfile.STATE_DISCONNECTED); verify(gatt).close(); verify(internalCallback).disconnected(any(BluetoothPeripheral.class), anyInt()); }
Example 8
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 5 votes |
@Test public void connectionPriorityTest() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); peripheral.requestConnectionPriority(CONNECTION_PRIORITY_HIGH); verify(gatt).requestConnectionPriority(CONNECTION_PRIORITY_HIGH); }
Example 9
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 5 votes |
@Test public void writeDescriptor() { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_INDICATE,0); BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002903-0000-1000-8000-00805f9b34fb"),0); service.addCharacteristic(characteristic); characteristic.addDescriptor(descriptor); when(gatt.getServices()).thenReturn(Arrays.asList(service)); byte[] originalByteArray = new byte[]{0x01}; peripheral.writeDescriptor(descriptor, originalByteArray); verify(gatt).writeDescriptor(descriptor); callback.onDescriptorWrite(gatt, descriptor, 0); ArgumentCaptor<BluetoothPeripheral> captorPeripheral = ArgumentCaptor.forClass(BluetoothPeripheral.class); ArgumentCaptor<byte[]> captorValue = ArgumentCaptor.forClass(byte[].class); ArgumentCaptor<BluetoothGattDescriptor> captorDescriptor = ArgumentCaptor.forClass(BluetoothGattDescriptor.class); ArgumentCaptor<Integer> captorStatus = ArgumentCaptor.forClass(Integer.class); verify(peripheralCallback).onDescriptorWrite(captorPeripheral.capture(), captorValue.capture(), captorDescriptor.capture(), captorStatus.capture()); byte[] value = captorValue.getValue(); assertEquals(0x01, value[0]); assertNotEquals(value, originalByteArray); // Check if the byte array has been copied assertEquals(peripheral, captorPeripheral.getValue()); assertEquals(descriptor, captorDescriptor.getValue()); assertEquals(GATT_SUCCESS, (int) captorStatus.getValue() ); }
Example 10
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 5 votes |
@Test public void readDescriptor() { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_INDICATE,0); BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002903-0000-1000-8000-00805f9b34fb"),0); service.addCharacteristic(characteristic); characteristic.addDescriptor(descriptor); when(gatt.getServices()).thenReturn(Arrays.asList(service)); peripheral.readDescriptor(descriptor); verify(gatt).readDescriptor(descriptor); byte[] originalByteArray = new byte[]{0x01}; descriptor.setValue(originalByteArray); callback.onDescriptorRead(gatt, descriptor, 0); ArgumentCaptor<BluetoothPeripheral> captorPeripheral = ArgumentCaptor.forClass(BluetoothPeripheral.class); ArgumentCaptor<byte[]> captorValue = ArgumentCaptor.forClass(byte[].class); ArgumentCaptor<BluetoothGattDescriptor> captorDescriptor = ArgumentCaptor.forClass(BluetoothGattDescriptor.class); ArgumentCaptor<Integer> captorStatus = ArgumentCaptor.forClass(Integer.class); verify(peripheralCallback).onDescriptorRead(captorPeripheral.capture(), captorValue.capture(), captorDescriptor.capture(), captorStatus.capture()); byte[] value = captorValue.getValue(); assertEquals(0x01, value[0]); assertNotEquals(value, originalByteArray); // Check if the byte array has been copied assertEquals(peripheral, captorPeripheral.getValue()); assertEquals(descriptor, captorDescriptor.getValue()); assertEquals(GATT_SUCCESS, (int) captorStatus.getValue() ); }
Example 11
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 5 votes |
@Test public void queueTestConsecutiveReadsWithError() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0); BluetoothGattCharacteristic characteristic2 = new BluetoothGattCharacteristic(UUID.fromString("00002A1E-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0); service.addCharacteristic(characteristic); service.addCharacteristic(characteristic2); byte[] byteArray = new byte[] {0x01, 0x02, 0x03}; characteristic.setValue(byteArray); characteristic2.setValue(byteArray); when(gatt.readCharacteristic(characteristic)).thenReturn(true); peripheral.readCharacteristic(characteristic); peripheral.readCharacteristic(characteristic2); verify(gatt).readCharacteristic(characteristic); callback.onCharacteristicRead(gatt, characteristic, 128); verify(peripheralCallback, never()).onCharacteristicUpdate(peripheral, byteArray, characteristic,128); verify(gatt).readCharacteristic(characteristic2); callback.onCharacteristicRead(gatt, characteristic2, GATT_SUCCESS); verify(peripheralCallback).onCharacteristicUpdate(peripheral, byteArray ,characteristic2, 0); }
Example 12
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 5 votes |
@Test public void queueTestConsecutiveReads() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0); BluetoothGattCharacteristic characteristic2 = new BluetoothGattCharacteristic(UUID.fromString("00002A1E-0000-1000-8000-00805f9b34fb"),PROPERTY_READ,0); service.addCharacteristic(characteristic); service.addCharacteristic(characteristic2); byte[] byteArray = new byte[] {0x01, 0x02, 0x03}; characteristic.setValue(byteArray); characteristic2.setValue(byteArray); when(gatt.readCharacteristic(characteristic)).thenReturn(true); peripheral.readCharacteristic(characteristic); peripheral.readCharacteristic(characteristic2); verify(gatt).readCharacteristic(characteristic); callback.onCharacteristicRead(gatt, characteristic, GATT_SUCCESS); verify(peripheralCallback).onCharacteristicUpdate(peripheral, byteArray, characteristic, 0); verify(gatt).readCharacteristic(characteristic2); callback.onCharacteristicRead(gatt, characteristic2, GATT_SUCCESS); verify(peripheralCallback).onCharacteristicUpdate(peripheral, byteArray, characteristic2, 0); }
Example 13
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 5 votes |
@Test public void writeCharacteristicTest() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"), PROPERTY_WRITE,0); when(gatt.writeCharacteristic(characteristic)).thenReturn(true); peripheral.writeCharacteristic(characteristic, new byte[]{0}, WRITE_TYPE_DEFAULT); verify(gatt).writeCharacteristic(any(BluetoothGattCharacteristic.class)); byte[] originalByteArray = new byte[]{0x01}; characteristic.setValue(originalByteArray); callback.onCharacteristicWrite(gatt, characteristic, 0); ArgumentCaptor<BluetoothPeripheral> captorPeripheral = ArgumentCaptor.forClass(BluetoothPeripheral.class); ArgumentCaptor<byte[]> captorValue = ArgumentCaptor.forClass(byte[].class); ArgumentCaptor<BluetoothGattCharacteristic> captorCharacteristic = ArgumentCaptor.forClass(BluetoothGattCharacteristic.class); ArgumentCaptor<Integer> captorStatus = ArgumentCaptor.forClass(Integer.class); verify(peripheralCallback).onCharacteristicWrite(captorPeripheral.capture(), captorValue.capture(), captorCharacteristic.capture(), captorStatus.capture()); byte[] value = captorValue.getValue(); assertEquals(0, value[0]); // Check if original value is returned and not the one in the characteristic assertEquals(peripheral, captorPeripheral.getValue()); assertEquals(characteristic, captorCharacteristic.getValue()); assertEquals(GATT_SUCCESS, (int) captorStatus.getValue() ); }
Example 14
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 5 votes |
@Test public void readCharacteristicTest() throws Exception { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"), PROPERTY_READ,0); characteristic.setValue(new byte[]{0x00}); when(gatt.readCharacteristic(characteristic)).thenReturn(true); peripheral.readCharacteristic(characteristic); verify(gatt).readCharacteristic(any(BluetoothGattCharacteristic.class)); byte[] originalByteArray = new byte[]{0x01}; characteristic.setValue(originalByteArray); callback.onCharacteristicRead(gatt, characteristic, 0); ArgumentCaptor<BluetoothPeripheral> captorPeripheral = ArgumentCaptor.forClass(BluetoothPeripheral.class); ArgumentCaptor<byte[]> captorValue = ArgumentCaptor.forClass(byte[].class); ArgumentCaptor<BluetoothGattCharacteristic> captorCharacteristic = ArgumentCaptor.forClass(BluetoothGattCharacteristic.class); ArgumentCaptor<Integer> captorStatus = ArgumentCaptor.forClass(Integer.class); verify(peripheralCallback).onCharacteristicUpdate(captorPeripheral.capture(), captorValue.capture(), captorCharacteristic.capture(), captorStatus.capture()); byte[] value = captorValue.getValue(); assertEquals(0x01, value[0]); assertNotEquals(value, originalByteArray); // Check if the byte array has been copier assertEquals(peripheral, captorPeripheral.getValue()); assertEquals(characteristic, captorCharacteristic.getValue()); assertEquals(GATT_SUCCESS, (int) captorStatus.getValue() ); }
Example 15
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 4 votes |
@Test public void autoconnectTest() throws Exception { peripheral.autoConnect(); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); ArgumentCaptor<BluetoothGattCallback> captor = ArgumentCaptor.forClass(BluetoothGattCallback.class); ArgumentCaptor<Boolean> autoConnectCaptor = ArgumentCaptor.forClass(Boolean.class); verify(device).connectGatt(any(Context.class), autoConnectCaptor.capture(), captor.capture(), anyInt()); boolean autoconnect = autoConnectCaptor.getValue(); assertTrue(autoconnect); BluetoothGattCallback callback = captor.getValue(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); assertEquals(STATE_CONNECTED, peripheral.getState()); }
Example 16
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 3 votes |
@Test public void readRemoteRSSITest() { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); peripheral.readRemoteRssi(); verify(gatt).readRemoteRssi(); callback.onReadRemoteRssi(gatt, -40, 0); verify(peripheralCallback).onReadRemoteRssi(peripheral, -40, 0); }
Example 17
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 3 votes |
@Test public void requestMTUTest() { BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED); peripheral.requestMtu(32); verify(gatt).requestMtu(32); callback.onMtuChanged(gatt, 32, 0); verify(peripheralCallback).onMtuChanged(peripheral, 32, 0); }
Example 18
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 3 votes |
@Test public void onConnectionStateChangedConnectedAlreadyBondedTest() throws Exception { when(device.getBondState()).thenReturn(BOND_BONDED); BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, BluetoothProfile.STATE_CONNECTED); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); verify(gatt).discoverServices(); }
Example 19
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 3 votes |
@Test public void onConnectionStateChangedConnectedUnbondedTest() throws Exception { when(device.getBondState()).thenReturn(BOND_NONE); BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, BluetoothProfile.STATE_CONNECTED); verify(gatt).discoverServices(); }
Example 20
Source File: BluetoothPeripheralTest.java From blessed-android with MIT License | 3 votes |
@Test public void onConnectionStateChangedConnectedBondedingTest() throws Exception { when(device.getBondState()).thenReturn(BOND_BONDING); BluetoothGattCallback callback = connectAndGetCallback(); callback.onConnectionStateChange(gatt, GATT_SUCCESS, BluetoothProfile.STATE_CONNECTED); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); verify(gatt, never()).discoverServices(); }