Android Tutorial Android Bluetooth / BluetoothAdmin Tutorial

Status
Not open for further replies.
Better implementation based on B4XPages:
Tutorial was rewritten in April 2018.
We will create a chat example between two Android devices.

All the non-UI code is implemented in a class named BluetoothManager. It is initialized in Service_Create of the starter service.

It is always better to implement communication related code in a service or a class initialized from a service. This way the communication state is not affected by the activities more complicated state.

The activities call the class methods directly. Calling the activities subs is done with CallSub. Remember that CallSub doesn't do anything if the activity is paused.

The first activity, the main activity, shows two buttons. One for searching other devices and one for listening for connections.

Searching other devices requires a "dangerous" permissions (see the runtime permissions tutorial for more information).
Note that you need to add the permission in the manifest editor:
B4X:
AddPermission(android.permission.ACCESS_FINE_LOCATION)

B4X:
Sub btnSearchForDevices_Click
   rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   If Result = False Then
       ToastMessageShow("No permission...", False)
       Return
   End If
   Starter.Manager.SearchForDevices
End Sub

The second button sends an intent to the OS to make the device discoverable and then calls Serial.Listen.

Once a connection is established:
1. AsyncStreams is initialized in prefix mode (see the AsyncStreams tutorial for more information).
2. The ChatActivity is started.

SS-2018-04-04_12.02.01.jpg


Notes

- In this example we send text messages. Use B4XSerializator to send more complex types.
 

Attachments

  • Bluetooth.zip
    12.4 KB · Views: 7,071
Last edited:

Scantech

Well-Known Member
Licensed User
Longtime User
Are you sure that there are no bluetooth devices near you?

The DeviceFound event is raised as a result of the native API. It will only add devices reported by the OS.
I will do some more investigating. I was in the apartment when I did a search. Not Sure how many other Bluetooth devices are around me. When Away from the apartments it is working properly.
 
Last edited:

techknight

Well-Known Member
Licensed User
Longtime User
Why they would put that in the locations permission is completely asinine, because this could lead into a false sense of "insecurity" when installing an app and someone actually looks at the permissions. "Why does it need to know my location?" phrase... GRRR google....

Anyways... Can someone try on Android M if location is disabled, if BT discovery still works?
 

DavideV

Active Member
Licensed User
Longtime User
Why they would put that in the locations permission is completely asinine, because this could lead into a false sense of "insecurity" when installing an app and someone actually looks at the permissions. "Why does it need to know my location?" phrase... GRRR google....

Anyways... Can someone try on Android M if location is disabled, if BT discovery still works?

I agree with you about the permissions, too much for bluetooth discovery in my opinion.
Could be for ibeacon?
isn't it a sort of bluetooth localization?
 

Mitesh_Shah

Member
Licensed User
Longtime User
HI CAN ANY ONE TRY IN NEW ANDROID OS 6.0 ?

I AM TRY IT IN MY LENOVO K3 NOTE PHONE - ANDROID 6.0 IT'S NOT WORKING. GETTING ERROR DEVICE NOT FOUND

BUT IT'S WORK IN KITKAT AND LOLLIPOP Version

REGARD

MITESH
 

Mitesh_Shah

Member
Licensed User
Longtime User
HI Erel

I m add >>android.permission.ACCESS_COARSE_LOCATION to Region Module Attributes but, getting Compiling Error.

Parsing code. Error
Error parsing program.
Error description: Attribute not supported: android
Occurred on line: 10
#Android:permission.ACCESS_COARSE_LOCATION
 

red30

Well-Known Member
Licensed User
Longtime User
I use your example. The data is transmitted. Not working reception ... What am I doing wrong?
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

'Activity module
Sub Process_Globals
    Dim AStream As AsyncStreams
    Dim bc As ByteConverter
End Sub

Sub Globals
    Dim readings As EditText
    Dim label1 As Label


End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("2")
    readings.Color = Colors.black
    If AStream.IsInitialized = False Then
        'AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
        AStream.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStreams")
    End If
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    readings.Text=Buffer
    label1.Text=("Data: " & bc.HexFromBytes(Buffer))
End Sub

Sub AStream_Error
    ToastMessageShow("Connection is broken.", True)
End Sub

Sub AStream_Terminated
    AStream_Error
End Sub

Sub Activity_Resume
  
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        AStream.Close
    End If
End Sub

Sub LightOn_Click
    AStream.Write(Array As Byte(0x00,0x05,0x3f))
End Sub
 
Status
Not open for further replies.
Top