gpt4 book ai didi

android - 配对后如何连接到蓝牙设备?

转载 作者:行者123 更新时间:2023-11-29 17:16:08 26 4
gpt4 key购买 nike

我有一个用于配对和连接到蓝牙设备的类。我正在使用 Android 6.0 来实现它。该应用程序有两个按钮,用于配对和连接功能。它适用于配对功能。但是,我无法实现连接功能。我看了一些蓝牙连接的例子,但是当我在这节课中使用时它们不工作。请看看我的课,给我一些实现的方向?谢谢大家

enter image description here

public class DeviceListActivity extends Activity {
private ListView mListView;
private DeviceListAdapter mAdapter;
private ArrayList<BluetoothDevice> mDeviceList;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_paired_devices);

mDeviceList = getIntent().getExtras().getParcelableArrayList("device.list");

mListView = (ListView) findViewById(R.id.lv_paired);

mAdapter = new DeviceListAdapter(this);

mAdapter.setData(mDeviceList);
mAdapter.setPairListener(new DeviceListAdapter.OnPairButtonClickListener() {
@Override
public void onPairButtonClick(int position) {
BluetoothDevice device = mDeviceList.get(position);

if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
unpairDevice(device);
} else {
showToast("Pairing...");

pairDevice(device);
}
}

});
mAdapter.setConnectListener(new DeviceListAdapter.OnConnectButtonClickListener() {
@Override
public void onConnectButtonClick(int position) {
//Connect bluetooth
}

});
mListView.setAdapter(mAdapter);

registerReceiver(mPairReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
}

@Override
public void onDestroy() {
unregisterReceiver(mPairReceiver);

super.onDestroy();
}


private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

private void pairDevice(BluetoothDevice device) {
try {
Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}

private void unpairDevice(BluetoothDevice device) {
try {
Method method = device.getClass().getMethod("removeBond", (Class[]) null);
method.invoke(device, (Object[]) null);

} catch (Exception e) {
e.printStackTrace();
}
}
private void connectDevice(BluetoothDevice device) {
// Connect bluetooth
}

private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);

if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {
showToast("Paired");
} else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){
showToast("Unpaired");
}

mAdapter.notifyDataSetChanged();
}
}
};
}

最佳答案

作为客户端连接很简单。您首先通过调用 createRfcommSocketToServiceRecord() 从所需的 BluetoothDevice 获取 RFCOMM 套接字,传入一个 UUID,一个您创建的 128 位值。 UUID类似于端口号

如果你持有配对列表中的蓝牙设备,你可以只使用这个类来连接它,

public class ConnectThread extends Thread{
private BluetoothSocket bTSocket;

public boolean connect(BluetoothDevice bTDevice, UUID mUUID) {
BluetoothSocket temp = null;
try {
temp = bTDevice.createRfcommSocketToServiceRecord(mUUID);
} catch (IOException e) {
Log.d("CONNECTTHREAD","Could not create RFCOMM socket:" + e.toString());
return false;
}
try {
bTSocket.connect();
} catch(IOException e) {
Log.d("CONNECTTHREAD","Could not connect: " + e.toString());
try {
bTSocket.close();
} catch(IOException close) {
Log.d("CONNECTTHREAD", "Could not close connection:" + e.toString());
return false;
}
}
return true;
}

public boolean cancel() {
try {
bTSocket.close();
} catch(IOException e) {
Log.d("CONNECTTHREAD","Could not close connection:" + e.toString());
return false;
}
return true;
}
}

我建议使用本教程作为起点来学习和了解蓝牙在 android 中的工作原理。

http://code.tutsplus.com/tutorials/create-a-bluetooth-scanner-with-androids-bluetooth-api--cms-24084

关于android - 配对后如何连接到蓝牙设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38871814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com