I am working on a project where I need to connect to a bluetooth scanner(Motorola CS3070). I need to capture the input stream and use it to populate a list box with the scanned barcode. I tried to create a secure socket and connect to it, but the socket fails to connect. (The device is ON and paired. Its working as a physical keyboard and populates the scanned code if cursor is in a editable field). This is my code:
private static BluetoothAdapter bluetoothAdapter = null;
private const int REQUEST_ENABLE_BT = 2;
public static ParcelUuid UUID;
public static Dictionary<string, string> deviceDictionary = new Dictionary<string, string>();
public static int STATE = 0;
public static BluetoothSocket mySocket;
public static Activity _activity;
private static BluetoothReceiver receiver;
private static BluetoothDevice pairedBTDevice;
public void InitializeBluetooth()
{
// Get local Bluetooth adapter
bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
// If the adapter is null, then Bluetooth is not supported
if (bluetoothAdapter == null)
{
Toast.MakeText(this, "Bluetooth is not available", ToastLength.Long).Show();
Finish();
return;
}
// If bluetooth is not enabled, ask to enable it
if (!bluetoothAdapter.IsEnabled)
{
var enableBtIntent = new Intent(BluetoothAdapter.ActionRequestEnable);
StartActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
// Get connected devices
var listOfDevices = bluetoothAdapter.BondedDevices;
if (listOfDevices.Count > 0)
{
foreach (var bluetoothDevice in listOfDevices)
{
UUID = bluetoothDevice.GetUuids().ElementAt(0);
if (!deviceDictionary.ContainsKey(bluetoothDevice.Name))
deviceDictionary.Add(bluetoothDevice.Name, bluetoothDevice.Address);
}
}
else
{
connectButton.Text = "Scanner Not connected";
connectButton.Clickable = false;
}
if (bluetoothAdapter.IsEnabled && listOfDevices.Count > 0)
{
if (listOfDevices.ElementAt(0).BondState == Bond.Bonded)
{
pairedBTDevice = listOfDevices.ElementAt(0);
mySocket = pairedBTDevice.CreateRfcommSocketToServiceRecord(UUID.Uuid);
var thread = new Thread(BTConnect);
thread.Start();
}
}
}
public static void BTConnect()
{
try
{
mySocket.Connect();
_activity.RunOnUiThread(() => connectButton.Text = " Scanner Connected");
_activity.RunOnUiThread(() => connectButton.Clickable = false);
receiver.loop = true;
var thread = new Thread(BTRead, "BTRead");
thread.Start();
}
catch (Exception e)
{
_activity.RunOnUiThread(() => Toast.MakeText(_activity.ApplicationContext, "Couldn't connect. Is the device on and paired?", ToastLength.Long).Show());
_activity.RunOnUiThread(() => connectButton.Text = "Scanner Not connected");
_activity.RunOnUiThread(() => connectButton.Clickable = true);
receiver.loop = false;
}
}
The code gives an exception at the line mySocket.Connect();
The exception is Java.IO.IOException: "read failed, socket might closed or timeout, read ret: -1"
.
I also tried to create a fallback socket with some example I found on StackOverflow here but that didn't help me. Can someone please help me resolve this.
Thanks
Edit* My application has the following bluetooth permissions:
- android.permission.BLUETOOTH_ADMIN
- android.permission.BLUETOOTH