我正在尝试将我的应用程序与外部蓝牙条码扫描器(YHD-3100)连接,但当我尝试从BluetoothSocket调用.connect()时,我一直收到java.io.IOException: read failed, socket might closed or timeout, read ret: -1
。
当我连接无线打印机(能够打印出来没有问题)时,此代码工作正常但是条形码扫描仪失败了。这可能是什么原因?
这是我的代码:
public class BluetoothConnect implements Runnable {
// Code eliminated
public BluetoothConnect(Context context, int device) {
// Code eliminated
}
public String retrieveBarcode() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter.isEnabled()) {
Log.d(TAG, "Bluetooth enabled");
getBluetoothList(device, true);
} else {
Log.d(TAG, "Bluetooth disabled");
alertDialog(TITLE_BLUETOOTH_OFF, MESSAGE_BLUETOOTH_OFF);
}
return barcode;
}
private void getBarcode() {
Log.d(TAG, "Attempt to retrieve barcode");
Thread thread = new Thread() {
public void run() {
try {
InputStream inputStream = bluetoothSocket.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line).append('\n');
}
barcode = sb.toString();
} catch (Exception e) {
Log.e(TAG, "Unable to retrieve barcode", e);
}
}
};
thread.start();
}
private void getBluetoothList(int device, boolean isExecute) {
Log.d(TAG, "Retrieve list of bluetooth");
Set<BluetoothDevice> pairedSet = bluetoothAdapter.getBondedDevices();
int size = pairedSet.size();
if(size > 0) {
ArrayList<String> bluetoothArray = new ArrayList<>();
for(BluetoothDevice bluetoothDevice : pairedSet) {
// Only display in list for wireless printer devices
if(bluetoothDevice != null && bluetoothDevice.getBluetoothClass().getDeviceClass() == device) {
pairedDevices.add(bluetoothDevice);
bluetoothArray.add(bluetoothDevice.getName());
Log.d(TAG, "Device: " + bluetoothDevice.getName());
}
}
bluetoothList = bluetoothArray.toArray(new CharSequence[bluetoothArray.size()]);
alertBluetoothListDialog();
}
}
private void alertBluetoothListDialog() {
Log.d(TAG, "Open dialog for bluetooth list");
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.ic_attach_money_black_24dp);
if(device == TYPE_PRINTER) {
builder.setTitle(TITLE_BLUETOOTH_PRINTER);
} else if(device == TYPE_BARCODE_SCANNER) {
builder.setTitle(TITLE_BLUETOOTH_SCANNER);
}
builder.setItems(bluetoothList, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
bluetoothDevice = pairedDevices.get(which);
setBluetoothItems();
dialog.dismiss();
}
});
builder.show();
}
private void setBluetoothItems() {
bluetoothAdapter.cancelDiscovery();
deviceMacAddress = bluetoothDevice.getAddress();
// Retrieve device's first UUID
ParcelUuid[] list = bluetoothDevice.getUuids();
if(list[0] != null) {
deviceUUID = list[0].toString();
}
connectAppToDevice();
}
private void connectAppToDevice() {
Log.d(TAG, "Attempt to connect app to device");
bluetoothDevice = bluetoothAdapter.getRemoteDevice(deviceMacAddress);
bluetoothConnectProgressDialog = ProgressDialog.show(context,
"Connecting...", bluetoothDevice.getName(), true, false);
Thread bluetoothConnectThread = new Thread(this);
bluetoothConnectThread.start();
}
private void alertDialog(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.ic_warning_24dp);
builder.setTitle(title);
builder.setMessage(message);
builder.setNegativeButton(MESSAGE_OK, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
public void run() {
try {
UUID appUUID = UUID.fromString(deviceUUID);
bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(appUUID);
bluetoothAdapter.cancelDiscovery();
bluetoothSocket.connect();
handler.sendEmptyMessage(0);
} catch (IOException eConnectException) {
closeSocket(bluetoothSocket, true);
}
}
private void closeSocket(BluetoothSocket openSocket, boolean isError) {
try {
openSocket.close();
bluetoothConnectProgressDialog.dismiss();
} catch (IOException ex) {
Log.d(TAG, "Could not close socket");
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
bluetoothConnectProgressDialog.dismiss();
getBarcode();
closeSocket(bluetoothSocket, false);
}
};
}