I am scanning for a peripheral with a custom profile; this is found. But I am failing to connect with the device.
My function for connecting is.....
function scanListener( ScanResult ){ Ble.setScanState( Ble.SCAN_STATE_OFF ); System.println( "AvailableConnectionCount is "+ Ble.getAvailableConnectionCount() ); try{ myDevice = Ble.pairDevice( ScanResult ); System.println( "paired device " + myDevice ); if( myDevice instanceof Ble.Device ){ System.println( "paired IS A DEVICE " + myDevice.getName() ); System.println( "AvailableConnectionCount is "+ Ble.getAvailableConnectionCount() ); } if( myDevice.isConnected() ){ System.println( "reports Connected" ); } }catch( exception ){ System.println( exception.getErrorMessage() ); } }
My console output being....
initialising view
got scan result NOVA_31
AvailableConnectionCount is 3
paired device Obj: 164
paired IS A DEVICE null
AvailableConnectionCount is 3
profile registered 3F4E1400-D5C9-46C3-A2A5-090606023D85
I see no exception thrown; I see no calls of onConnectedStateChange(); the pairDevice() returns an object which is apparently a Ble.Device yet myDevice.Getname() returns null and myDevice.isConnected() returns false.
In my peripheral which is nordic based I see some debug output which kind of hints that a connection is made; but then dropped.
<info> app: systemProcessEvent SYSTEM_BLE_CONNECTED_EVT
<debug> nrf_ble_gatt: ATT MTU updated to 23 bytes on connection 0x0 (response).
<info> app: Data len is set to 0x14(20)
<debug> app: ATT MTU exchange completed. central 0xF7 peripheral 0xF7
<info> app: BLE event received. Event type = 58
<info> app: BLE event received. Event type = 18
<debug> app: Battery ADC event number: 12
<debug> app: Battery voltage: 4002
<debug> app: Battery percentage: 77%
<debug> app: Battery ADC event number: 13
<debug> app: Battery voltage: 4002
<debug> app: Battery percentage: 77%
<debug> app: Battery ADC event number: 14
<debug> app: Battery voltage: 4006
<debug> app: Battery percentage: 78%
<info> app: systemProcessEvent SYSTEM_BATTERY_DRAINING_EVT
<info> app: BLE event received. Event type = 18
<info> app: BLE_ADV_EVT_FAST event
<info> app: BLE event received. Event type = 17
<info> app: Disconnected
Going to have to get another Nordic dev kit to use as a BLE sniffer?
Is there anything else I can do?
This is my first attempt at programming using Monkey C; so I may be making some more basic mistakes. I tried to register a listener on the class that did the scan.
This passes the first ScanResult via the listener back to another class that does the connect.
class NovaApp extends Application.AppBase { var nble = new NovaBle(); var myDevice = null; function initialize() { AppBase.initialize(); Ble.setDelegate( nble ); nble.registerProfiles(); nble.registerScanResultListener( self ); Ble.setScanState( Ble.SCAN_STATE_SCANNING ); } function scanListener( ScanResult ){ Ble.setScanState( Ble.SCAN_STATE_OFF ); System.println( "AvailableConnectionCount is "+ Ble.getAvailableConnectionCount() ); try{ myDevice = Ble.pairDevice( ScanResult ); System.println( "paired device " + myDevice ); if( myDevice instanceof Ble.Device ){ System.println( "paired IS A DEVICE " + myDevice.getName() ); System.println( "AvailableConnectionCount is "+ Ble.getAvailableConnectionCount() ); } if( myDevice.isConnected() ){ System.println( "reports Connected" ); } }catch( exception ){ System.println( exception.getErrorMessage() ); } }
class NovaBle extends Ble.BleDelegate { var listener = null; function initialize() { BleDelegate.initialize(); } function registerScanResultListener( novaApp ){ listener = novaApp; } function onProfileRegister( uuid, status ) { System.println( "profile registered " + uuid.toString() ); } function onScanResults( scanResults ){ var item = scanResults.next(); while( item ){ if( item instanceof Ble.ScanResult ){ System.println( "got scan result " + item.getDeviceName() ); listener.scanListener( item ); break; } item = scanResults.next(); } } function onConnectedStateChange( device, state ){ System.println( device.getName() + " state " + state ); }
Have hit a wall. So hopefully one of you has some ideas.
Regards,
Owain