@Override public void onStateChange(Node node, Node.State newState, Node.State prevState) { if (newState == Node.State.Connected) { setUpConsoleService(node.getDebug()); } else if (newState == Node.State.Dead) { DebugConsoleActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(DebugConsoleActivity.this, R.string.DebugNotAvailable, Toast.LENGTH_SHORT).show(); } }); }//if-else }//onStateChange };//NodeStateListener
/** * build the menu and show the item only if the service is available in the node * * @param menu menu where add the items */ @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_demo, menu); menu.findItem(R.id.menu_showDebug).setVisible(mNode.getDebug() != null); menu.findItem(R.id.menu_showRegister).setVisible(mNode.getConfigRegister() != null); return true; }//onCreateOptionMenu
/** * if the node is connected enable the gui otherwise attach a listener for do it when the node * connects */ @Override protected void onResume() { super.onResume(); if (mNode.isConnected()) { setUpConsoleService(mNode.getDebug()); } else mNode.addNodeStateListener(mNodeStateChangeListener); }//onResume
@Test public void connectNodeWithDebug(){ BluetoothDevice device = spy(Shadow.newInstanceOf(BluetoothDevice.class)); BluetoothDeviceShadow shadowDevice = Shadow.extract(device); BluetoothGattService debugService = new BluetoothGattService(BLENodeDefines.Services .Debug.DEBUG_SERVICE_UUID,BluetoothGattService.SERVICE_TYPE_PRIMARY); debugService.addCharacteristic( new BluetoothGattCharacteristic(BLENodeDefines.Services.Debug.DEBUG_STDERR_UUID, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE, BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic .PROPERTY_NOTIFY)); debugService.addCharacteristic( new BluetoothGattCharacteristic(BLENodeDefines.Services.Debug.DEBUG_TERM_UUID, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE, BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic .PROPERTY_NOTIFY) ); shadowDevice.addService(debugService); Node node = createNode(device); Assert.assertEquals(Node.State.Idle, node.getState()); node.connect(RuntimeEnvironment.application); TestUtil.execAllAsyncTask(); verify(device).connectGatt(eq(RuntimeEnvironment.application), eq(false), any(BluetoothGattCallback.class)); Assert.assertEquals(Node.State.Connected, node.getState()); Assert.assertTrue(node.getDebug()!=null); }