I'm trying to connect to a Bluetooth device to receive barcode scan data (Using Visual Studio 2019, connected to an Android device running KitKat OS).
I've tried many tutorials and examples without any luck... Here's a few I used as a guidance :
- https://acaliaro.wordpress.com/2017/02/07/connect-a-barcode-reader-to-a-xamarin-forms-app-via-bluetooth/
- https://stackoverflow.com/questions/46433911/xamarin-forms-read-failed-socket-might-closed-or-timeout-read-ret-1
- https://stackoverflow.com/questions/29121724/creating-and-destroying-bluetooth-sockets
When I do the following:
socket = device.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
socket.Connect();
I get an error of: Java.IO.IOException: read failed, socket might closed or timeout, read ret: -1
So I thought, maybe I should be using a listener instead?:
serversocket = adapter.ListenUsingRfcommWithServiceRecord(device.Name, UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
socket = serversocket.Accept();
But it hangs at serversocket.Accept(), believe me... it was stuck there for over 10 minutes until I killed the app.
To confirm I wasn't going crazy, I tested the Bluetooth device on a machine which had the manufacturers testing app installed; and it worked as expected... but not in my app.
So... what is the correct way for accessing Bluetooth without it falling over all the time?
All I want to do is listen for data being scanned by a Bluetooth device and then parse that information to one of my methods to process. Am I supposed to use both CreateRfcommSocketToServiceRecord and ListenUsingRfcommWithServiceRecord both together? Or, is the Bluetooth feature just flaky in Android???
I need to get this feature working as Zebra EMDK doesn't support non Zebra/Motorola devices, and Bluetooth would be the best go-to method if I could only get it to work.
Manifest has the following settings:
- android.permission.BLUETOOTH_ADMIN
- android.permission.BLUETOOTH
- android.permission.ACCESS_COARSE_LOCATION
Please help, this is driving me totally insane!