Attempt to invoke virtual method 'void android.bluetooth.BluetoothGatt.close()'

562 views
Skip to first unread message

Skyler

unread,
Oct 5, 2017, 3:46:16 PM10/5/17
to mitappinv...@googlegroups.com

I have a code base where I could successfully connect to bluetooth but it had a leak/stack issue of some kind which was causing slowdowns and subsequent crashes. This is that original set of blocks for that:


 After applying a few fixes and running the app in app inventor, the first time I connect to the device I receive an error stating "Not connected to a Bluetooth low energy device" however often on the second or third try I would receive a different error stating: "Error 3300: Error 9007 in extension BluetoothLE: You cannot connect to a device when the device list is empty! Try scanning again." This time around though the device actually connects via BLE upon receiving that error and I can successful my (string) data from my device. This is the block there:

I tried to go after this device list error with some further (minor) reorganizing of my code base, as you can see in the block here:

So with this relatively minor change for some reason my BLE refuses to connect outright, on first connection I still get that "Not connected to a Bluetooth low energy device" error on first connection. Repeated connections either display no error eventually I see a new error: " Attempt to invoke virtual method 'void android.bluetooth.BluetoothGatt.close()' on a null object reference". 

My most recent change I tried adding in when BluetoothLE1.Disconnected a call Bluetooth.StopScanning method, and now after 5-15 connections I started getting the "Error 3300: Error 9007 in extension BluetoothLE: You cannot connect to a device when the device list is empty! Try scanning again." again. I also added a small bit of logic that displays Connected or Disconnected in their respective while loops. However, this display reads Connected when the device isn't reading the data and Disconnected when the device is streaming me data.  What the heck is going on and what should I be doing to fix these errors?


gerrikoio

unread,
Oct 6, 2017, 11:21:03 AM10/6/17
to MIT App Inventor Forum
I think you may be overcomplicating things a bit.

To start, not sure a "touchdown" method on the listpicker is the best way to initialise a scan because once you release it will immediately involve the "afterpicking" call. So you need to place a check of some sort to make sure there is devices available otherwise error. Then it is not always the case that your app will pick up only a single ble device and that it is first in the list. So... It is better to use a button to initiate a ble scan.

Then connecting to a BLE device...

There is no need to go to all this trouble.

Simply use the index of the list to connect.

Then the error as noted in the title of this post... the clue is the following "null object reference"

That means there is nothing there. You are initiating a ble connect call when there are still no ble devices discovered. This is because of my first point (incorrectly using touchdown method).

Skyler

unread,
Oct 9, 2017, 10:11:28 PM10/9/17
to MIT App Inventor Forum
Hey gerrikoio. I had already gotten rid of the touchdown by the time this error started showing up so its not related to the touchdown. The last image would be the accurate reference for that error.

gerrikoio

unread,
Oct 10, 2017, 6:58:54 AM10/10/17
to MIT App Inventor Forum
@Skyler. Aha. Didn't know that. Had a quick glance. Looks like you are registering for BLE strings (within the AfterPicking block) before BLE is confirmed connected. Move that to BLE is connected block.
Reply all
Reply to author
Forward
0 new messages