[英]How to get android connected(not paired) bluetooth devices?
將android手機連接到藍牙設備后,無法搜索該設備。 如何將藍牙設備連接到手機?我嘗試了幾種在線可用的無效方法。
這是我正在使用的代碼。
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Class<BluetoothAdapter> bluetoothAdapterClass =
BluetoothAdapter.class;
try {
Method method =
bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);
method.setAccessible(true);
int state = (int) method.invoke(adapter, (Object[]) null);
if(state == BluetoothAdapter.STATE_CONNECTED){
Log.i("BLUETOOTH","BluetoothAdapter.STATE_CONNECTED");
Set<BluetoothDevice> devices = adapter.getBondedDevices();
Log.i("BLUETOOTH","devices:"+devices.size());
for(BluetoothDevice device : devices){
Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
method.setAccessible(true);
boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
if(isConnected){
Log.i("BLUETOOTH","connected:"+device.getName());
deviceList.add(device);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
這是我的代碼:有時候這可能對您有幫助。
首先,您應該檢查藍牙是否已與設備連接,
if (isDeviceConnected) {
//do something
} else {
//do something...
}
如果未連接,則可以使用,
if (!mService.isBTopen()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else {
if (isDeviceConnected) {
Toast.makeText(getContext(), "Already Connected", Toast.LENGTH_SHORT).show();
} else {
Intent serverIntent = new Intent(getContext(), DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
}
}
這是onActivityResult,用於檢查您的藍牙連接和連接的設備。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("Hello", " Device connection received!");
switch (requestCode) {
case REQUEST_ENABLE_BT: //���������
if (resultCode == Activity.RESULT_OK) { //�����Ѿ���
Intent serverIntent = new Intent(getContext(), DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
} else { //�û������������
// finish();
}
break;
case REQUEST_CONNECT_DEVICE: //��������ijһ�����豸
if (resultCode == Activity.RESULT_OK) { //�ѵ�������б��е�ij���豸��
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); //��ȡ�б������豸��mac��ַ
BluetoothDevice con_dev = mService.getDevByMac(address);
String addressM = address;
mService.connect(con_dev);
}
break;
}
}
這是hanlder類:
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BluetoothService.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothService.STATE_CONNECTED: //������
Toast.makeText(getContext(), "Connect successful",
Toast.LENGTH_SHORT).show();
isDeviceConnected = true;
// updateConnectBtn();
// btnClose.setEnabled(true);
// btnSendDraw.setEnabled(true);
break;
case BluetoothService.STATE_CONNECTING: //��������
Log.d("��������", "��������.....");
break;
case BluetoothService.STATE_LISTEN: //�������ӵĵ���
case BluetoothService.STATE_NONE:
Log.d("��������", "�ȴ�����.....");
break;
}
break;
case BluetoothService.MESSAGE_CONNECTION_LOST: //�����ѶϿ�����
Toast.makeText(getContext(), "Device connection was lost",
Toast.LENGTH_SHORT).show();
isDeviceConnected = false;
// btnClose.setEnabled(false);
// btnSendDraw.setEnabled(false);
break;
case BluetoothService.MESSAGE_UNABLE_CONNECT: //�������豸
Toast.makeText(getContext(), "Unable to connect device",
Toast.LENGTH_SHORT).show();
break;
}
}
};
處理程序總是檢查您是否連接,如果您已連接,它將更改isDeviceConnected = true;
否則isDeviceConnected = false;
如果已連接,則可以從設備列表中更改設備,否則可以繼續使用同一設備。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.