What is an example of connecting to an already paired bluetooth device from an android app (android 12)?
The main method I've found is using sockets:
var d = dl[0]
var u = d.uuids.get(0)!!.uuid
bleSocket = d.createInsecureRfcommSocketToServiceRecord(u)
ble.cancelDiscovery()
bleSocket?.connect()
However, with this method I get an io exception called as a method under MainActivity via a button.
The weird thing is that it only happens on some UUID of the same device (the source UUID).
Three of questions, do I need to have a dedicated thread for the socket?
Is there a way (ex, intent) that would essentially be same as clicking the connect button with a paired bluetooth device under the "connected devices"? (because that works fine with manually connecting)
Finally, the error I get is "Caused by: java.io.IOException: read failed, socket might closed or timeout, read ret: -1". What does it mean?
On the receiver side, I see it connect and shortly after disconnect.
Related
I've a bluetooth gatt service and two channels to write
bluetoothGattService = gatt?.getService(UUID.fromString(ble_gatt_service_uuid))
mTxCharacteristic1 = bluetoothGattService.getCharacteristic(UUID.fromString(txCharacteristics1))
mTxCharacteristic2 = bluetoothGattService.getCharacteristic(UUID.fromString(txCharacteristics2))
Now I have onCharacteristicsRead(), onCharacteristicsWrite(), and onCharacteristicsChange()
I connected with the ble device
When I write on mTxCharacteristic1 characteristic then ble works well
But whenever I write on mTxCharacteristic2 characteristic then ble gets DISCONNECTED
// function1
mTxCharacteristic1.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
mTxCharacteristic1.value = chunk
gatt.writeCharacteristic(mTxCharacteristic1)
//function 2
mTxCharacteristic2.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
mTxCharacteristic2.value = chunk
gatt.writeCharacteristic(mTxCharacteristic2)
How to resolve this?
Everytime it is getting disconnected whenever I send data using 2nd characteristics, I'm only able to connect with ble again only when I restart my android device.
I tried:
gatt.beginReliableWrite()
gatt.setCharacteristicNotification(characteristic1,true)
gatt.writeCharacteristic(characteristic1)
gatt.executeReliableWrite()
gatt.beginReliableWrite()
gatt.setCharacteristicNotification(characteristic2,true)
gatt.writeCharacteristic(characteristic2)
gatt.executeReliableWrite()
nrf connect app showing (TX(1) and unknown_characteristic(2))
still getting the same disconnected with gatt server issue with state 133.
Just made this simple writeCharacteristics instead of reliableWrite on doing this disconnection and no repairing issue resolved
gatt.writeCharacteristic(characteristic1)
gatt.writeCharacteristic(characteristic2)
I was have 8 byte of packet data where I sent null which I found on debugging
expected was 19 byte array and I was sending only 11 packets (8 missed)
due to this it was giving me error and getting disconnected with state code 133
I'm having issues when trying to use bluetooth on my project. On previous version I used the HC-05 module for bluetooth and it gives me a simple serial connection right after any device connects. Now the project evolved and we opted on using SIM800H because it gives us GSM+Bluetooth.
When I connect any android device it gives me some profiles but never SPP. I can pair normally but when using any SPP server app (https://stackoverflow.com/a/4037619/2637661) I can never send or get data from my device. If I start the connection from the Android app, it says that it's connecting while the SIM800 gives me the URC and I respond:
+BTCONNECTING: "34:c7:31:aa:37:5b","SPP"
AT+BTACPT=1
OK
+BTCONNECT: 1,"Android",34:c7:31:aa:37:5b,"SPP"
But it stays on server mode and I can't use the commands AT+BTSPPSEND or AT+BTSPPGET, as the documentation says.
On the other hand, if I start the connection from my device just like the docs say:
AT+BTCONNECT=1,4 // Device is 1 and SPP profile is 4
OK
AT+BTSPPSEND
>I type anything here + ctrl+z
SEND FAIL
and get nothing on the Android side.
Plus in both cases the connection drops after something like 30s and I can't reestablish it unless I turn the SIM800H off and on again.
Got no success using the APP mode either (sending the string "SIMCOMSPPFORAPP" right after connection succeeds for transparent communication).
The SIM800H firmware version is
AT+CGMR
Revision:1309B07SIM800H32_BT
and I tried using the following apps
Bluetooth Terminal
Bluetooth spp pro
BlueSPP
The GSM side works flawlessly and I can send/receive TCP messages everytime I try.
Does anyone have any kind of experience using this module? Thanks for reading!
UPDATE:
I'm using a simple sketch in order to talk with the module's serial, don't know if it's relevant but here it goes.
#define SIM800_POWER 23
void setup() {
Serial.begin(9600);
Serial1.begin(19200);
Serial.print("Setting all up");
pinMode(SIM800_POWER, OUTPUT);
Serial.print(".");
delay(500);
digitalWrite(SIM800_POWER, HIGH);
Serial.print(".");
Serial.println("OK");
}
void loop() {
if(Serial1.available()){
Serial.write(Serial1.read());
}
if(Serial.available()){
Serial1.write(Serial.read());
}
}
And what I get after trying Andrii's answer:
Setting all up..OK
AT
OK
AT
OK
AT
OK
AT+BTPOWER=0
OK
AT+BTPOWER=1
OK
AT+BTCONNECT=1,4
OK
+BTCONNECT: 1,"Will",d4:87:d8:77:37:0b,"SPP"
AT+BTSTATUS?
+BTSTATUS: 5
P: 1,"Will",d4:87:d8:77:37:0b
C: 1,"Will",d4:87:d8:77:37:0b,"SPP"
OK
AT+BTSPPSEND
> SIMCOMSPPFORAPP
SEND FAIL
Seems AT+BTSPPSEND without any parameters is only for AT-command send from client (your SIM800H) to server (other SIM800, not your Android device unless your Android device implements AT-command support). For data sending you should use AT+BTSPPSEND and after receiving > symbol send SIMCOMSPPFORAPP keyword and then, after receiving SEND OK response send command AT+BTSPPSEND=<LENGTH_OF_YOUR_DATA> and then, after receiving > symbol, send your data until Ctrl+Z code e.g.:
AT+BTSPPSEND
> SIMCOMSPPFORAPP
SEND OK
AT+BTSPPSEND=5
> HELLO
SEND OK
^Z
where HELLO - is your data, and 5 in AT+BTSPPSEND=5 is length of HELLO string. Details in SIM800H_BT_Application_Note.
UPDATE
Selected by bold small, but important part of answer (thanks to hlovdal)
I use a thread to send data to my usb-device (not data-storage) and get the return-data from device.
It worked fine in the first 5 minute. Data in, data out. (I almost start to sing in the sixth minute.)
Then suddenly it lost the connection.
There are the logcat message:
java.lang.IllegalArgumentException: device /dev/bus/usb/001/002 does not exist or is restricted
at android.os.Parcel.readException(Parcel.java:1331)
at android.os.Parcel.readException(Parcel.java:1281)
at android.hardware.usb.IUsbManager$Stub$Proxy.openDevice(IUsbManager.java:340)
at android.hardware.usb.UsbManager.openDevice(UsbManager.java:255)
So, I check the code of UsbManager, trying to find out what is the IUsbManager...
Cool. An interface... and there is zero information about what it is. That is why I am here.
Has anyone with any clue???
The app should be listening to the UsbManager.ACTION_USB_DEVICE_ATTACHED and UsbManager.ACTION_USB_DEVICE_DETACHED Intents. It should attempt to open the device received from the attached intent.
The error usually happens when the app tries to re-open the connection to a UsbDevice. When the UsbDevice reconnects it is assigned a new hardware address (Old: /dev/bus/usb/001/002, New: /dev/bus/usb/001/003).
I use this code
luugiathuy.com/2011/02/android-java-bluetooth/
The server side is the PC
the client is the device, with the app based on bluetooth chat example
The device (galaxy tab 7.0) can establish connection with the PC.
However the PC server (written in java and bluecove) did nothing, as nothing is connected.
The loop for trying to find connected device is
while(true) {
try {
System.out.println("waiting for connection...");
connection = notifier.acceptAndOpen();
Thread processThread = new Thread(new ProcessConnectionThread(connection));
processThread.start();
} catch (Exception e) {
e.printStackTrace();
return;
}
Output on PC:
uuid: 0000110100001000800000805f9b34fb
waiting for connection...
EDIT: source downloadhttps://github.com/luugiathuy/Remote-Bluetooth-Android
Same issue I got when I was trying in linux. But the reason (still not sure) when you run the bluetooth android application without turning on the Java server using bluecove, It will try to connect with the already installed bluetooth software. You may see the bluetooth icon asking for granting access to the mobile device.
To solve this, I just changed the uuid in the server and application (say from 1103 to 1101 and vice versa) and then started the server first and then the android application. Java server part started listening.
The reason I think may be the uuid when it did not found the bluecove stack service server, it got connected to the device server listening on same uuid. So after changing the uuid and making sure that the server is running before launching the android application should solve the issue.
If you are getting connected to the bluetooth system application and not to the Java bluecove server,
1) First change the uuid both server and android application.
2) Second make sure your server is running and listening on same uuid.
3) Launch the android application which try to communicate on same rfcomm connection uuid.
Server part code I took from : http://www.jsr82.com/jsr-82-sample-spp-server-and-client/
Library : http://code.google.com/p/bluecove/downloads/list
Yes, it happens with me too, I suggest you to fire following commend on shell, when it shows waiting for connection.
hcitool cc 58:C3:8B:D7:FA:F4
here 58:C3:8B:D7:FA:F4 is my device's bluetooth address, which should be replaced by your device's bluetooth address.
To get your device's bluetooth address, just start bluetooth in your device with discoverable mode and execute hcitool scan command, it will display all the active device with their name and bluetooth address.
Well you may run the above hcitool cc 58:C3:8B:D7:FA:F4 command via Java code as follows,
try
{
Process p=Runtime.getRuntime().exec("hcitool cc 58:C3:8B:D7:FA:F4");
}
catch ( Exception e )
{
}
The output from your program says it listens on UUID 0x1101. Is that true? The sample you reference shows it listening on a different UUID. Its Service Class Id is 0x04c6093b and is set as follows:
34 UUID uuid = new UUID(80087355); // "04c6093b-0000-1000-8000-00805f9b34fb"
35 String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
36 notifier = (StreamConnectionNotifier)Connector.open(url);
The two need to match on client and server.
when opening a bluetooth rfcomm socket via the UUID method ( the other method ( using reflection ) in here http://code.google.com/p/android/issues/detail?id=5427 ) - I sometimes get the following error ( after successfull connections ) and the socket is not opened:
E/BluetoothService(21847): Received ACTION_UPDATE_SERVICE_CACHE00:0B:CE:01:2E:00
D/BluetoothService(21847): updateDeviceServiceChannelCache(00:0B:CE:01:2E:00)
D/BluetoothService(21847): Cleaning up failed UUID channel lookup: 00:0B:CE:01:2E:00 00001101-0000-1000-8000-00805f9b34fb
had no luck with google on this one - has anyone here a clue whats going on and how to prevent that?
Update:
for people running into the same problem - possible solutions:
advice the users to reboot the device if we run into that state (
very bad UX )
use the reflection method ( dirty but works most of the time )
I am still open for new Ideas ;-)
I stick to #2 at the moment - but waiting for a better solution.
It seems that the message is the consequence, not the reason. If a serial port emulation service is not found on device the connection will obviously fail.
Update
After reviewing code everything seems ok. So my questions are: Do the random errors occur with same device o with different devices? If the occur with different devices, the devices might have not registered the SerialPort service. Is device previously paired? In case of the error ocurring randomly with same device? Are you using the connection class concurrently? You have not protected the methods against concurrent access. You have not protected either against succesive connects. If you try to read/close a stream after a second connect it will fail (the socket object has changed).
It seems ok. The random error occurs with same device? If this is the case, are you using concurrent use of that class? In first case