Android Question BLE Device Name Error

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using the Bluetooth Low Energy (BLE) library and when it finds devices I have it logging the name and Mac address in the IDE Log like the following:

B4X:
Sub ble_DeviceFound (Name As String, MacAddress As String)
  
   log("Name = " & Name & " - Mac = " & MacAddress)
End Sub

I have noticed that in my area I have devices that don't seem to have a name and it logs the Name in the IDE as Null.

I am trying to filter out any that have that name and can't seem to work it out.

I have tried using the following code:
B4X:
Sub ble_DeviceFound (Name As String, MacAddress As String)
    If Name = "null" Then
        Log("Device has no name")
        Return
    End If
   Log("Name = " & Name & " - Mac = " & MacAddress)
End Sub

But I get the error:
B4X:
bluetoothservice_ble_devicefound (B4A line: 36)
If Name = "null" Then
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at b4a.example.bluetoothservice._ble_devicefound(bluetoothservice.java:193)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA$3.run(BA.java:334)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

If I don't use the following code then it works fine except it still logs 'null'
B4X:
If Name = "null" Then
    Log("Device has no name")
    Return
End If

Anyone know why it throws this error ?
 

MaFu

Well-Known Member
Licensed User
Longtime User
If the device doesn't have a name the variable "Name" is not initialized and therefore Null. But you compared it with a string named "null".
Use this comparison instead:
B4X:
If Name = Null Then
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
If the device doesn't have a name the variable "Name" is not initialized and therefore Null. But you compared it with a string named "null".
Use this comparison instead:
B4X:
If Name = Null Then
Thanks, I tried everything but that.

Working fine now, thanks for your help.
 
Upvote 0
Top