Java Android Studio-自动发送蓝牙PIN
我正在看以下内容 我看过其他关于配对时自动向设备发送PIN码(例如1234)的帖子,这样就没有用户提示了。我不知道如何添加他们的代码片段来让它工作 如何修改下面的代码来实现这一点Java Android Studio-自动发送蓝牙PIN,java,android,bluetooth,android-bluetooth,Java,Android,Bluetooth,Android Bluetooth,我正在看以下内容 我看过其他关于配对时自动向设备发送PIN码(例如1234)的帖子,这样就没有用户提示了。我不知道如何添加他们的代码片段来让它工作 如何修改下面的代码来实现这一点 /** * Device list. * * @author Lorensius W. L. T <lorenz@londatiga.net> * */ public class DeviceListActivity extends Activity { private ListView
/**
* Device list.
*
* @author Lorensius W. L. T <lorenz@londatiga.net>
*
*/
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.setListener(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);
}
}
});
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 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();
}
}
};
}
/**
*设备列表。
*
*@作者洛伦西斯·W·L·T
*
*/
公共类DeviceListActivity扩展了活动{
私有列表视图;
私有设备适配器;
私人ArrayList mDeviceList;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u配对设备);
mDeviceList=getIntent().getExtras().getParcelableArrayList(“device.list”);
mListView=(ListView)findViewById(R.id.lv_配对);
mAdapter=新设备适配器(本);
mAdapter.setData(mDeviceList);
mAdapter.setListener(新的DeviceListAdapter.OnPairButtonClickListener(){
@凌驾
公共无效onPairButtonClick(内部位置){
BluetoothDevice=mDeviceList.get(位置);
if(device.getBondState()==BluetoothDevice.BOND\u BOND){
非配对装置(装置);
}否则{
展示土司(“配对…”);
配对设备(装置);
}
}
});
mListView.setAdapter(mAdapter);
registerReceiver(mPairReceiver,新的意向过滤器(BluetoothDevice.ACTION\u BOND\u STATE\u CHANGED));
}
@凌驾
公共空间{
未注册接收人(MPAIReceiver);
super.ondestory();
}
私有void showtoos(字符串消息){
Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT.show();
}
专用无效配对设备(蓝牙设备){
试一试{
方法Method=device.getClass().getMethod(“createBond”,(Class[])null;
调用(设备,(对象[])null);
}捕获(例外e){
e、 printStackTrace();
}
}
私有无效未配对设备(蓝牙设备){
试一试{
方法Method=device.getClass().getMethod(“removeBond”,(Class[])null;
调用(设备,(对象[])null);
}捕获(例外e){
e、 printStackTrace();
}
}
private final BroadcastReceiver mPairReceiver=新的BroadcastReceiver(){
公共void onReceive(上下文、意图){
String action=intent.getAction();
如果(BluetoothDevice.ACTION\u BOND\u STATE\u CHANGED.equals(ACTION)){
final int state=intent.getIntExtra(BluetoothDevice.EXTRA\u BOND\u state,BluetoothDevice.ERROR);
final int prevState=intent.getIntExtra(BluetoothDevice.EXTRA\u PREVIOUS\u BOND\u STATE,BluetoothDevice.ERROR);
if(state==BluetoothDevice.BOND\u BONDING&&prevState==BluetoothDevice.BOND\u BONDING){
展示土司(“配对”);
}else if(state==BluetoothDevice.BOND\u NONE&&prevState==BluetoothDevice.BOND\u BOND){
展示土司(“未配对”);
}
mAdapter.notifyDataSetChanged();
}
}
};
}