Bluetooth problem

stephankuba

Member
Licensed User
Longtime User
Hi,
i want to read from FB155 Bluetoothmodule (Hardware) with android on my galaxy SIII. The module sends every second as string. I don't know which connection type form serial i should use. I've tried all examples on this forum (.zip) but always no connection is possible. The same problem with my galaxy tab 2 10.1. I'm using basic4android v2.25. Better to use asynstream? I think it is a SPP connection.?

Maybe someone can help me. thank you

No connection with this:
B4X:
[ code ] Sub Admin_DiscoveryFinished
ProgressDialogHide
If foundDevices.Size = 0 Then
ToastMessageShow("No device found.", True)
Else
Dim l As List
l.Initialize
For i = 0 To foundDevices.Size - 1
Dim nm As NameAndMac
nm = foundDevices.Get(i)
'ListView1.AddSingleLine(nm.Name)
l.Add(nm.Name)
Next
Dim res As Int
res = InputList(l, "Choose device to connect", -1)
If res <> DialogResponse.CANCEL Then
connectedDevice = foundDevices.Get(res)

ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")

'serial1.Connect(connectedDevice.Mac)
'serial1.ConnectInsecure(admin,connectedDevice.Mac ,CInt(ListView1))
'serial1.Connect3(connectedDevice.Mac,CInt(ListVie w1))
serial1.ConnectInsecure(admin,connectedDevice.Mac, 1)
End If
End If
End Sub

Edit/Delete Message
 
Last edited:

stephankuba

Member
Licensed User
Longtime User
bluetooth connection problem

pair and unpair don't solve the problem, no connection (Blutetooth Admin Example don't work) but i've downloaded a App on playstore named "Uconnect BT" which is working, so my telefon works.

B4X:
serial1.ConnectInsecure(admin,connectedDevice.Mac, 1)

Error connecting: java.io.IOExeption: [JRS82] attempt: Connection is not created (failed or aborded)

All this tries to to connect dont work
B4X:
'serial1.Connect(connectedDevice.Mac)
'serial1.Connect3(connectedDevice.Mac,1)
serial1.ConnectInsecure(admin,connectedDevice.Mac, 1)

maybe someone has an idea?? Thank you:sign0085:
 
Last edited:
Upvote 0

raphael75

Active Member
Licensed User
Longtime User
Does the progress dialog "Searching for devices..." appear and disappear on the screen? Do you get the toast message "No device found."? Do you get a list of discovered Bluetooth devices?
 
Upvote 0

stephankuba

Member
Licensed User
Longtime User
The dialog "Searching for devices..." appears, i can choose a device, then the app want's to connect and than the error message comes. Somethimes the Activity 2 starts and then the message "program ends" comes, app will be ended.

I don't know why this doesn't work. Example form Bluetooth admin.

B4X:
'Activity module
Sub Process_Globals
   Dim admin As BluetoothAdmin
   Dim serial1 As Serial
   Dim foundDevices As List
   Type NameAndMac (Name As String, Mac As String)
   Dim connectedDevice As NameAndMac
End Sub

Sub Globals
   Dim btnSearchForDevices As Button
   Dim btnAllowConnection As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      admin.Initialize("admin")
      serial1.Initialize("serial1")
   End If
   Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
   btnSearchForDevices.Enabled = False
   btnAllowConnection.Enabled = False
   If admin.IsEnabled = False Then
      If admin.Enable = False Then 
         ToastMessageShow("Error enabling Bluetooth adapter.", True)
      Else
         ToastMessageShow("Enabling Bluetooth adapter...", False)
         'the StateChanged event will be soon raised
      End If
   Else
      Admin_StateChanged(admin.STATE_ON, 0)
   End If
End Sub

Sub Admin_StateChanged (NewState As Int, OldState As Int)
   btnSearchForDevices.Enabled = (NewState = admin.STATE_ON)
   btnAllowConnection.Enabled = btnSearchForDevices.Enabled
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed = True Then
      serial1.Disconnect
   End If
End Sub

Sub btnSearchForDevices_Click
   foundDevices.Initialize
   If admin.StartDiscovery   = False Then 
      ToastMessageShow("Error starting discovery process.", True)
   Else
      ProgressDialogShow("Searching for devices...")
   End If
End Sub

Sub Admin_DiscoveryFinished
   ProgressDialogHide
   If foundDevices.Size = 0 Then
      ToastMessageShow("No device found.", True)
   Else
      Dim l As List
      l.Initialize
      For i = 0 To foundDevices.Size - 1
         Dim nm As NameAndMac
         nm = foundDevices.Get(i)
         l.Add(nm.Name)
      Next
      Dim res As Int
      res = InputList(l, "Choose device to connect", -1)
      If res <> DialogResponse.CANCEL Then
         connectedDevice = foundDevices.Get(res)
         ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
         serial1.Connect(connectedDevice.Mac)
      End If
   End If
End Sub

Sub Admin_DeviceFound (Name As String, MacAddress As String)
   Log(Name & ":" & MacAddress)
   Dim nm As NameAndMac
   nm.Name = Name
   nm.Mac = MacAddress
   foundDevices.Add(nm)
   ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))
End Sub

Sub btnAllowConnection_Click
   'this intent makes the device discoverable for 300 seconds.
   Dim i As Intent
   i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
   i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
   StartActivity(i)
   
   serial1.Listen
End Sub

Sub Serial1_Connected (Success As Boolean)
   ProgressDialogHide
   Log("connected: " & Success)
   If Success = False Then
      Log(LastException.Message)
      ToastMessageShow("Error connecting: " & LastException.Message, True)
   Else
      StartActivity(ChatActivity)
   End If
End Sub

B4X:
'Activity module
Sub Process_Globals
   Dim AStream As AsyncStreams
End Sub

Sub Globals
   Dim txtInput As EditText
   Dim txtLog As EditText
   Dim btnSend As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("2")
   If AStream.IsInitialized = False Then
      AStream.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStreams")
   End If
   txtLog.Width = 100%x
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub AStream_Error
   ToastMessageShow("Connection is broken.", True)
   btnSend.Enabled = False
   txtInput.Enabled = False
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 txtInput_EnterPressed
   If btnSend.Enabled = True Then btnSend_Click
End Sub
Sub btnSend_Click
   AStream.Write(txtInput.Text.GetBytes("UTF8"))
   txtInput.SelectAll
   txtInput.RequestFocus
   LogMessage("Me", txtInput.Text)
End Sub

Sub LogMessage(From As String, Msg As String)
   txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
   txtLog.SelectionStart = txtLog.Text.Length
End Sub

Thank you for your help:sign0142:
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…