我想将我的 Android 设备 (android 4.2.2) 与另一个使用
BluetothAdapter.startDiscovery()
的 Android 设备配对。
我在运行
BluetoothDevice.connect()
时发现请求配对开始,但是当运行 connect()
时,我得到了
读取失败,socket可能关闭或超时,读取ret:-1
我尝试实现后备和gatclass,IOException中的方法:读取失败,套接字可能关闭 - Android 4.3上的蓝牙
还根据蓝牙连接失败“java.io.IOException:读取失败,套接字可能关闭或超时,读取ret:-1”来更正UUID
和静态UUID“00001101-0000-1000-8000-00805F9B34FB”
但它们都不适合我。
来自 Android 希望向其他设备发送蓝牙配对请求的以下代码与我的代码类似并重现了该问题:
public class Main extends Activity {
TextView out;
private static final int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter btAdapter;
private ArrayList < BluetoothDevice > btDeviceList = new ArrayList < BluetoothDevice > ();
private ArrayList < String > mylist = new ArrayList < String > ();
private ListView lv;
private Button btn;
public Parcelable[] uuidExtra;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void search(View view) {
//Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_UUID);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(ActionFoundReceiver, filter); // Don't forget to unregister during onDestroy
// Getting the Bluetooth adapter
btAdapter = BluetoothAdapter.getDefaultAdapter();
Toast.makeText(getApplicationContext(), "\nAdapter: " + btAdapter, 5000).show();
CheckBTState();
}
private void setDeviceList(ArrayList < String > list) {
lv = (ListView) findViewById(R.id.listView);
ArrayAdapter < String > adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
/* This routine is called when an activity completes.*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT) {
CheckBTState();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (btAdapter != null) {
btAdapter.cancelDiscovery();
}
unregisterReceiver(ActionFoundReceiver);
}
private void CheckBTState() {
// Check for Bluetooth support and then check to make sure it is turned on
// If it isn't request to turn it on
// List paired devices
// Emulator doesn't support Bluetooth and will return null
if (btAdapter == null) {
Toast.makeText(getApplicationContext(), "\nBluetooth NOT supported. Aborting.", 5000).show();
return;
} else {
if (btAdapter.isEnabled()) {
Toast.makeText(getApplicationContext(), "\nBluetooth is enabled...", 5000).show();
// Starting the device discovery
btAdapter.startDiscovery();
} else if (!btAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
/* else{
Intent intent = new Intent(btAdapter.ACTION_STATE_CHANGED);
startActivityForResult(intent, RESULT_CANCELED);
}*/
}
}
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(getApplicationContext(), "\n Device: " + device.getName() + ", " + device, 5000).show();
mylist.add(device.getName());
setDeviceList(mylist);
} else {
if (BluetoothDevice.ACTION_UUID.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
for (int i = 0; i < uuidExtra.length; i++) {
Toast.makeText(getApplicationContext(), "\n Device: " + device.getName() + ", " + device + ", Service: " + uuidExtra[i].toString(), 5000).show();
}
} else {
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Toast.makeText(getApplicationContext(), "\nDiscovery Started...", 5000).show();
} else {
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Toast.makeText(getApplicationContext(), "\nDiscovery Finished", 5000).show();
Iterator < BluetoothDevice > itr = btDeviceList.iterator();
while (itr.hasNext()) {
// Get Services for paired devices
BluetoothDevice device = itr.next();
Toast.makeText(getApplicationContext(), "\nGetting Services for " + device.getName() + ", " + device, 5000).show();
if (!device.fetchUuidsWithSdp()) {
Toast.makeText(getApplicationContext(), "\nSDP Failed for " + device.getName(), 5000).show();
}
}
}
}
}
}
}
};
}