如何把android蓝牙配对代码码PIN1234变成条码

android蓝牙配对 如何自动配对设置PIN码
该用户从未签到主题帖子e币
碰到同样的问题
该用户从未签到主题帖子e币
嗯,这个我正需要啊,好东西
该用户从未签到主题帖子e币
不错!这个也是我需要的,找了很久,希望正确
该用户从未签到主题帖子e币
aaaaaaaaaaaaaaaaaaaaa
该用户从未签到主题帖子e币
kanaknaknakankdnasdkas
该用户从未签到主题帖子e币
hellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
该用户从未签到主题帖子e币
忘记怎么解决的
该用户从未签到主题帖子e币
配对可以,但无法连接
请问如何实现自动配对?难道不会出现一个对话框让用户选择确认吗?
该用户从未签到主题帖子e币
接收不到ACTION_PAIRING_REQUEST 这个问题解决了没有啊
我的代码 好像也接受不到这个消息 ...
我的也不行。ACTION_PAIRING_REQUEST是在bluetooth中的还是device还是adapter?
签到天数: 212 天连续签到: 3 天[LV.7]常住居民III主题帖子e币
对我还是有点用的..
签到天数: 1 天连续签到: 1 天[LV.1]初来乍到主题帖子e币
本帖最后由 you_can_you_up 于
14:16 编辑
已解决,so easy!&&就用 网上的一个工具类ClsUtils,调用里面pair方法即可。建议别用广播,反应比较慢,出现等2秒才消失,另外当你想用手机配对其它设备时候也给你默认配对广播接受者里的配对码.
我的邮箱:&
求一个自动配对不弹配对窗口的Demo,万分感谢!&
该用户从未签到主题帖子e币
看了下源码,其实很简单。自己注册一个广播监听“android.bluetooth.device.action.PAIRING_REQUEST”,根据intent获得BluetoothDevice实例,然后调用setPairingConfirmation(true)方法就行了。如果调用不到可以用反射找到这个方法然后再调用。
PS:我是在4.0上跑的,大家可以交流下
您可以实现不弹窗自动配对吗?求一个自动配对不弹配对窗口的Demo,万分感谢!&
签到天数: 20 天连续签到: 1 天[LV.4]偶尔看看III主题帖子e币
已解决,so easy!&&就用 网上的一个工具类ClsUtils,调用里面pair方法即可。建议别用广播,反应比较慢,出 ...
求一个自动配对不弹配对窗口的Demo,万分感谢!
签到天数: 20 天连续签到: 1 天[LV.4]偶尔看看III主题帖子e币
看了下源码,其实很简单。自己注册一个广播监听“android.bluetooth.device.action.PAIRING_REQUEST”,根 ...
您可以实现不弹窗自动配对吗?求一个自动配对不弹配对窗口的Demo,万分感谢!
请问实现了吗 能否给个demo&
签到天数: 20 天连续签到: 1 天[LV.4]偶尔看看III主题帖子e币
已解决,so easy!&&就用 网上的一个工具类ClsUtils,调用里面pair方法即可。建议别用广播,反应比较慢,出 ...
我的邮箱:
推荐阅读热门话题
61887420384328284281261252226218208204201200715
1&小时前3&小时前5&小时前昨天&23:57昨天&22:08昨天&17:54昨天&17:35昨天&17:31昨天&17:21昨天&11:17昨天&10:46前天&23:46前天&17:52前天&17:39前天&15:56前天&14:54
Powered by
扫一扫 关注eoe官方微信后使用快捷导航没有帐号?
查看: 14224|回复: 7
求助:蓝牙耳机配对时不能选择输入配对码?
该用户从未签到
机型:asus n61& &acer4745g
系统:win7 64位旗舰
在蓝牙的添加设备中找到耳机时选中,点下一步后系统就开使连接,只后就说pin码错误…
中间应改有一个pin码相关的界面,可以使用默认,或手动输入的…没有出来,不知道怎么在配对时输入啊~
耳机是SONY的
该用户从未签到
耳机型号是SONY DR-BT101
该用户从未签到
耳机默认的是 0000
但不知道这四个零怎么输入
该用户从未签到
耳机默认的是 0000
但不知道这四个零怎么输入
该用户从未签到
主要是连pin码什么的都没问,系统就开始连了
该用户从未签到
想手动输入Pin码应该怎么办啊?…
该用户从未签到
求助啊…会是系统问题吗?
不想重装啊…
该用户从未签到
没折腾过耳机
鼠标和手机都没问题
17年6月精华宗师
17年6月精华大师
关注本友会
本友会微信公众号
VR微信公众号
benyouhui2012
Powered by3234人阅读
Android(106)
转自: http://blog.csdn.net/jilong17/article/details/7928628
&&&& 我看了几个文章,主要是接受配对广播,然后设置pin,实现配对,但是网上的大部分手机是不可以的,android.bluetoothdevice 下 action_pair_request ,没有定义这个,开始困扰了我一点时间,实现难度:是否能进入那个广播响应。
定义了一个类,这个是网上的可以直接用
//================================================================================================================================
package&zicox.&&&&import&java.lang.reflect.M&&import&java.lang.reflect.F&&&&&&import&android.bluetooth.BluetoothA&&import&android.bluetooth.BluetoothD&&import&android.util.L&&&&public&class&ClsUtils&&&{&&&&&&&&&&&&&&&static&public&boolean&createBond(Class&btClass,&BluetoothDevice&btDevice)&&&&&&throws&Exception&&&&&&{&&&&&&&&&&Method&createBondMethod&=&btClass.getMethod(&createBond&);&&&&&&&&&&Boolean&returnValue&=&(Boolean)&createBondMethod.invoke(btDevice);&&&&&&&&&&return&returnValue.booleanValue();&&&&&&}&&&&&&&&&&&&&&&&&&static&public&boolean&removeBond(Class&btClass,&BluetoothDevice&btDevice)&&&&&&&&&&&&&&throws&Exception&&&&&&{&&&&&&&&&&Method&removeBondMethod&=&btClass.getMethod(&removeBond&);&&&&&&&&&&Boolean&returnValue&=&(Boolean)&removeBondMethod.invoke(btDevice);&&&&&&&&&&return&returnValue.booleanValue();&&&&&&}&&&&&&&&&static&public&boolean&setPin(Class&btClass,&BluetoothDevice&btDevice,&&&&&&&&&&&&&&String&str)&throws&Exception&&&&&&{&&&&&&&&&&try&&&&&&&&&&{&&&&&&&&&&&&&&Method&removeBondMethod&=&btClass.getDeclaredMethod(&setPin&,&&&&&&&&&&&&&&&&&&&&&&new&Class[]&&&&&&&&&&&&&&&&&&&&&&{byte[].class});&&&&&&&&&&&&&&Boolean&returnValue&=&(Boolean)&removeBondMethod.invoke(btDevice,&&&&&&&&&&&&&&&&&&&&&&new&Object[]&&&&&&&&&&&&&&&&&&&&&&{str.getBytes()});&&&&&&&&&&&&&&Log.e(&returnValue&,&&&&+&returnValue);&&&&&&&&&&}&&&&&&&&&&catch&(SecurityException&e)&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&}&&&&&&&&&&catch&(IllegalArgumentException&e)&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&}&&&&&&&&&&catch&(Exception&e)&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&}&&&&&&&&&&return&true;&&&&&&&&&}&&&&&&&&&&&&&&&static&public&boolean&cancelPairingUserInput(Class&btClass,&&&&&&&&&&&&&&BluetoothDevice&device)&&&&&&&&&throws&Exception&&&&&&{&&&&&&&&&&Method&createBondMethod&=&btClass.getMethod(&cancelPairingUserInput&);&&&&&&&&&&&&&&&&&&&&Boolean&returnValue&=&(Boolean)&createBondMethod.invoke(device);&&&&&&&&&&return&returnValue.booleanValue();&&&&&&}&&&&&&&&&&&&&&&static&public&boolean&cancelBondProcess(Class&btClass,&&&&&&&&&&&&&&BluetoothDevice&device)&&&&&&&&&throws&Exception&&&&&&{&&&&&&&&&&Method&createBondMethod&=&btClass.getMethod(&cancelBondProcess&);&&&&&&&&&&Boolean&returnValue&=&(Boolean)&createBondMethod.invoke(device);&&&&&&&&&&return&returnValue.booleanValue();&&&&&&}&&&&&&&&&&&&&&&&&&static&public&void&printAllInform(Class&clsShow)&&&&&&{&&&&&&&&&&try&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&Method[]&hideMethod&=&clsShow.getMethods();&&&&&&&&&&&&&&int&i&=&0;&&&&&&&&&&&&&&for&(;&i&&&hideMethod.&i++)&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&Log.e(&method&name&,&hideMethod[i].getName()&+&&;and&the&i&is:&&&&&&&&&&&&&&&&&&&&&&&&&&&+&i);&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&Field[]&allFields&=&clsShow.getFields();&&&&&&&&&&&&&&for&(i&=&0;&i&&&allFields.&i++)&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&Log.e(&Field&name&,&allFields[i].getName());&&&&&&&&&&&&&&}&&&&&&&&&&}&&&&&&&&&&catch&(SecurityException&e)&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&}&&&&&&&&&&catch&(IllegalArgumentException&e)&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&}&&&&&&&&&&catch&(Exception&e)&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&}&&&&&&}&&&&&&&&&&&&&&&&&&&&&&static&public&boolean&pair(String&strAddr,&String&strPsw)&&&&&&{&&&&&&&&&&boolean&result&=&false;&&&&&&&&&&BluetoothAdapter&bluetoothAdapter&=&BluetoothAdapter&&&&&&&&&&&&&&&&&&.getDefaultAdapter();&&&&&&&&&&&&&bluetoothAdapter.cancelDiscovery();&&&&&&&&&&&&&if&(!bluetoothAdapter.isEnabled())&&&&&&&&&&{&&&&&&&&&&&&&&bluetoothAdapter.enable();&&&&&&&&&&}&&&&&&&&&&&&&if&(!BluetoothAdapter.checkBluetoothAddress(strAddr))&&&&&&&&&&{&&&&&&&&&&&&&&&&&&Log.d(&mylog&,&&devAdd&un&effient!&);&&&&&&&&&&}&&&&&&&&&&&&&BluetoothDevice&device&=&bluetoothAdapter.getRemoteDevice(strAddr);&&&&&&&&&&&&&if&(device.getBondState()&!=&BluetoothDevice.BOND_BONDED)&&&&&&&&&&{&&&&&&&&&&&&&&try&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&Log.d(&mylog&,&&NOT&BOND_BONDED&);&&&&&&&&&&&&&&&&&&ClsUtils.setPin(device.getClass(),&device,&strPsw);&&&&&&&&&&&&&&&&&&&ClsUtils.createBond(device.getClass(),&device);&&&&&&&&&&&&&&&&&&&&result&=&true;&&&&&&&&&&&&&&}&&&&&&&&&&&&&&catch&(Exception&e)&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Log.d(&mylog&,&&setPiN&failed!&);&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&&&}&&&&&&&&&&&&&&}&&&&&&&&&&else&&&&&&&&&&{&&&&&&&&&&&&&&Log.d(&mylog&,&&HAS&BOND_BONDED&);&&&&&&&&&&&&&&try&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&ClsUtils.createBond(device.getClass(),&device);&&&&&&&&&&&&&&&&&&ClsUtils.setPin(device.getClass(),&device,&strPsw);&&&&&&&&&&&&&&&&&&&ClsUtils.createBond(device.getClass(),&device);&&&&&&&&&&&&&&&&&&&&result&=&true;&&&&&&&&&&&&&&}&&&&&&&&&&&&&&catch&(Exception&e)&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Log.d(&mylog&,&&setPiN&failed!&);&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&&&}&&&&&&&&&&}&&&&&&&&&&return&&&&&&&}&&}&&
//================================================================================================================================
还有一部分 activity
//================================================================================================================================
package&zicox.&&&&import&java.io.IOE&&import&java.io.UnsupportedEncodingE&&import&java.lang.reflect.M&&import&java.util.ArrayL&&import&java.util.L&&import&java.util.UUID;&&&&import&android.app.A&&import&android.bluetooth.BluetoothA&&import&android.bluetooth.BluetoothD&&import&android.bluetooth.BluetoothS&&import&android.content.BroadcastR&&import&android.content.C&&import&android.content.I&&import&android.content.IntentF&&import&android.os.B&&import&android.util.L&&import&android.view.V&&import&android.widget.AdapterV&&import&android.widget.ArrayA&&import&android.widget.B&&import&android.widget.ListV&&import&android.widget.T&&import&android.widget.ToggleB&&&&&&public&class&Demo_ad_escActivity&extends&Activity&&{&&&&&&&&&&&&public&static&String&ErrorM&&&&&&Button&btnSearch,&btnDis,&btnE&&&&&&ToggleButton&tbtnS&&&&&&&&ListView&lvBTD&&&&&&&&ArrayAdapter&String&&adtD&&&&&&&&List&String&&lstDevices&=&new&ArrayList&String&();&&&&&&&&BluetoothAdapter&btA&&&&&&&&public&static&BluetoothSocket&btS&&&&&&&&&&&&&&&&&&&public&void&onCreate(Bundle&savedInstanceState)&{&&&&&&&&&&super.onCreate(savedInstanceState);&&&&&&&&&&setContentView(R.layout.main);&&&&&&&&&&&&&&&&&&Button&Button1&=&(Button)&findViewById(R.id.button1);&&&&&&&&&&ErrorMessage&=&&&;&&&&&&&&&&&&&&&&&&&&&&btnSearch&=&(Button)&this.findViewById(R.id.btnSearch);&&&&&&&&&&&&btnSearch.setOnClickListener(new&ClickEvent());&&&&&&&&&&&&btnExit&=&(Button)&this.findViewById(R.id.btnExit);&&&&&&&&&&&&btnExit.setOnClickListener(new&ClickEvent());&&&&&&&&&&&&btnDis&=&(Button)&this.findViewById(R.id.btnDis);&&&&&&&&&&&&btnDis.setOnClickListener(new&ClickEvent());&&&&&&&&&&&&&&&&&&&&&&tbtnSwitch&=&(ToggleButton)&this.findViewById(R.id.toggleButton1);&&&&&&&&&&&&tbtnSwitch.setOnClickListener(new&ClickEvent());&&&&&&&&&&&&&&&&&&&&lvBTDevices&=&(ListView)&this.findViewById(R.id.listView1);&&&&&&&&&&&&adtDevices&=&new&ArrayAdapter&String&(this,&&&&&&&&&&&&&&&&&&&&android.R.layout.simple_list_item_1,&lstDevices);&&&&&&&&&&&&lvBTDevices.setAdapter(adtDevices);&&&&&&&&&&&&lvBTDevices.setOnItemClickListener(new&ItemClickEvent());&&&&&&&&&&&&&&&&&&&&&&btAdapt&=&BluetoothAdapter.getDefaultAdapter();&&&&&&&&&&&&&&&&&&&&if&(btAdapt.isEnabled())&&&&&&&&&&&&&&&tbtnSwitch.setChecked(false);&&&&&&&&&&&&else&&&&&&&&&&&&&&&tbtnSwitch.setChecked(true);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&String&ACTION_PAIRING_REQUEST&=&&android.bluetooth.device.action.PAIRING_REQUEST&;&&&&&&&&&&IntentFilter&intent&=&new&IntentFilter();&&&&&&&&&&&&intent.addAction(BluetoothDevice.ACTION_FOUND);&&&&&&&&&&intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);&&&&&&&&&&&&intent.addAction(ACTION_PAIRING_REQUEST);&&&&&&&&&&intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);&&&&&&&&&&&&intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);&&&&&&&&&&&&registerReceiver(searchDevices,&intent);&&&&&&&&&&&&&&&&&&&&&&Button1.setOnClickListener(new&Button.OnClickListener()&&&&&&&&&&{&&&&&&&&&&&&&&public&void&onClick(View&arg0)&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&});&&&&&&}&&&&&&&&&&&&&&&&private&BroadcastReceiver&searchDevices&=&new&BroadcastReceiver()&{&&&&&&&&&&&&&&&&&&&&&&&&public&void&onReceive(Context&context,&Intent&intent)&{&&&&&&&&&&&&&&&&String&action&=&intent.getAction();&&&&&&&&&&&&&&&&Bundle&b&=&intent.getExtras();&&&&&&&&&&&&&&&&Object[]&lstName&=&b.keySet().toArray();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&for&(int&i&=&0;&i&&&lstName.&i++)&{&&&&&&&&&&&&&&&&&&&&String&keyName&=&lstName[i].toString();&&&&&&&&&&&&&&&&&&&&Log.e(keyName,&String.valueOf(b.get(keyName)));&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&BluetoothDevice&device&=&null;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if&(BluetoothDevice.ACTION_FOUND.equals(action))&{&&&&&&&&&&&&&&&&&&&&device&=&intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);&&&&&&&&&&&&&&&&&&&&if&(device.getBondState()&==&BluetoothDevice.BOND_NONE)&{&&&&&&&&&&&&&&&&&&&&&&&&String&str&=&&未配对|&&+&device.getName()&+&&|&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&+&device.getAddress();&&&&&&&&&&&&&&&&&&&&&&&&if&(lstDevices.indexOf(str)&==&-1)&&&&&&&&&&&&&&&&&&&&&&&&&&lstDevices.add(str);&&&&&&&&&&&&&&&&&&&&&&&adtDevices.notifyDataSetChanged();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&try&{&&&&&&&&&&&&&&&&&&&&&&&&&&ClsUtils.setPin(device.getClass(),device,&0000&);&&&&&&&&&&&&&&&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&try&{&&&&&&&&&&&&&&&&&&&&&&&&&&ClsUtils.cancelPairingUserInput(device.getClass(),&device);&&&&&&&&&&&&&&&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&}else&if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){&&&&&&&&&&&&&&&&&&&&device&=&intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);&&&&&&&&&&&&&&&&&&&&switch&(device.getBondState())&{&&&&&&&&&&&&&&&&&&&&case&BluetoothDevice.BOND_BONDING:&&&&&&&&&&&&&&&&&&&&&&&&Log.d(&BlueToothTestActivity&,&&正在配对......&);&&&&&&&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&&&&&&&case&BluetoothDevice.BOND_BONDED:&&&&&&&&&&&&&&&&&&&&&&&&Log.d(&BlueToothTestActivity&,&&完成配对&);&&&&&&&&&&&&&&&&&&&&&&&&connect(device);&&&&&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&&&&&&&case&BluetoothDevice.BOND_NONE:&&&&&&&&&&&&&&&&&&&&&&&&Log.d(&BlueToothTestActivity&,&&取消配对&);&&&&&&&&&&&&&&&&&&&&default:&&&&&&&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if&(intent.getAction().equals(&android.bluetooth.device.action.PAIRING_REQUEST&))&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Log.e(&tag&,&&ddd&);&&&&&&&&&&&&&&&&&&BluetoothDevice&btDevice&=&intent&&&&&&&&&&&&&&&&&&&&&&&&&&.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&try&&&&&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&ClsUtils.setPin(btDevice.getClass(),&btDevice,&&0000&);&&&&&&&&&&&&&&&&&&&&&&&ClsUtils.createBond(btDevice.getClass(),&btDevice);&&&&&&&&&&&&&&&&&&&&&&ClsUtils.cancelPairingUserInput(btDevice.getClass(),&btDevice);&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&catch&(Exception&e)&&&&&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&}&&&&&&&&&&}&&&&&&};&&&&&&&&&&&&class&ItemClickEvent&implements&AdapterView.OnItemClickListener&{&&&&&&&&&&&&&&&&&&&&&&&&@Override&&&&&&&&&&&&public&void&onItemClick(AdapterView&?&&arg0,&View&arg1,&int&arg2,&&&&&&&&&&&&&&&&&&&&long&arg3)&{&&&&&&&&&&&&&&&&if(btAdapt.isDiscovering())btAdapt.cancelDiscovery();&&&&&&&&&&&&&&&&String&str&=&lstDevices.get(arg2);&&&&&&&&&&&&&&&&String[]&values&=&str.split(&\\|&);&&&&&&&&&&&&&&&&String&address&=&values[2];&&&&&&&&&&&&&&&&Log.e(&address&,&values[2]);&&&&&&&&&&&&&&&&BluetoothDevice&btDev&=&btAdapt.getRemoteDevice(address);&&&&&&&&&&&&&&&&try&{&&&&&&&&&&&&&&&&&&&&Boolean&returnValue&=&false;&&&&&&&&&&&&&&&&&&&&if&(btDev.getBondState()&==&BluetoothDevice.BOND_NONE)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&ClsUtils.pair(address,&&0000&);&&&&&&&&&&&&&&&&&&&&&&showMessage(&here&);&&&&&&&&&&&&&&&&&&}else&if(btDev.getBondState()&==&BluetoothDevice.BOND_BONDED){&&&&&&&&&&&&&&&&&&&&&&&&connect(btDev);&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&&&&&}&&&&&&&&&&&&}&&&&&&&&}&&&&&&&&&&&&&&&&private&void&connect(BluetoothDevice&btDev)&{&&&&&&&&&&&final&UUID&SPP_UUID&=&UUID.fromString(&0-805F9B34FB&);&&&&&&&&&&UUID&uuid&=&SPP_UUID;&&&&&&&&&&&&try&{&&&&&&&&&&&&&&&&btSocket&=&btDev.createRfcommSocketToServiceRecord(uuid);&&&&&&&&&&&&&&&&Log.d(&BlueToothTestActivity&,&&开始连接...&);&&&&&&&&&&&&&&&&btSocket.connect();&&&&&&&&&&&&}&catch&(IOException&e)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&}&&&&&&&&}&&&&&&&&&&&&class&ClickEvent&implements&View.OnClickListener&{&&&&&&&&&&&&@Override&&&&&&&&&&&&public&void&onClick(View&v)&{&&&&&&&&&&&&&&&&if&(v&==&btnSearch)&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&if&(btAdapt.getState()&==&BluetoothAdapter.STATE_OFF)&{&&&&&&&&&&&&&&&&&&&&&&Toast.makeText(Demo_ad_escActivity.this,&&请先打开蓝牙&,&1000).show();&&&&&&&&&&&&&&&&&&&&return;&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&if&(btAdapt.isDiscovering())&&&&&&&&&&&&&&&&&&&&&&&&btAdapt.cancelDiscovery();&&&&&&&&&&&&&&&&&&&&lstDevices.clear();&&&&&&&&&&&&&&&&&&&&Object[]&lstDevice&=&btAdapt.getBondedDevices().toArray();&&&&&&&&&&&&&&&&&&&&for&(int&i&=&0;&i&&&lstDevice.&i++)&{&&&&&&&&&&&&&&&&&&&&&&&&BluetoothDevice&device&=&(BluetoothDevice)&lstDevice[i];&&&&&&&&&&&&&&&&&&&&&&&&String&str&=&&已配对|&&+&device.getName()&+&&|&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&+&device.getAddress();&&&&&&&&&&&&&&&&&&&&&&&&lstDevices.add(str);&&&&&&&&&&&&&&&&&&&&&&&adtDevices.notifyDataSetChanged();&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&setTitle(&本机蓝牙地址:&&+&btAdapt.getAddress());&&&&&&&&&&&&&&&&&&&&btAdapt.startDiscovery();&&&&&&&&&&&&&&&&}&else&if&(v&==&tbtnSwitch)&{&&&&&&&&&&&&&&&&&&if&(tbtnSwitch.isChecked()&==&false)&&&&&&&&&&&&&&&&&&&&&&&&btAdapt.enable();&&&&&&&&&&&&&&&&&&&&&&&&else&if&(tbtnSwitch.isChecked()&==&true)&&&&&&&&&&&&&&&&&&&&&&&&btAdapt.disable();&&&&&&&&&&&&&&&&}&else&if&(v&==&btnDis)&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&Intent&discoverableIntent&=&new&Intent(&&&&&&&&&&&&&&&&&&&&&&&&&&&&BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);&&&&&&&&&&&&&&&&&&&&discoverableIntent.putExtra(&&&&&&&&&&&&&&&&&&&&&&&&&&&&BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,&300);&&&&&&&&&&&&&&&&&&&&startActivity(discoverableIntent);&&&&&&&&&&&&&&&&}&else&if&(v&==&btnExit)&{&&&&&&&&&&&&&&&&&&&&try&{&&&&&&&&&&&&&&&&&&&&&&&&if&(btSocket&!=&null)&&&&&&&&&&&&&&&&&&&&&&&&&&&&btSocket.close();&&&&&&&&&&&&&&&&&&&&}&catch&(IOException&e)&{&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&Demo_ad_escActivity.this.finish();&&&&&&&&&&&&&&&&}&&&&&&&&&&&&}&&&&&&&&}&&&&&&&&&&&@Override&&&&&&&&protected&void&onDestroy()&{&&&&&&&&&&&&this.unregisterReceiver(searchDevices);&&&&&&&&&&&&super.onDestroy();&&&&&&&&&&&&android.os.Process.killProcess(android.os.Process.myPid());&&&&&&&&}&&&&&&&&public&void&showMessage(String&str)&&&&&&{&&&&&&&&&&Toast.makeText(this,str,&Toast.LENGTH_LONG).show();&&&&&&}&&&&&&&&}&&
//================================================================================================================================
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:339038次
积分:3138
积分:3138
排名:第10776名
原创:43篇
转载:107篇
评论:11条
(4)(1)(1)(4)(4)(3)(2)(2)(4)(1)(1)(1)(2)(7)(3)(1)(32)(38)(9)(17)(13)如何实现android蓝牙开发&自动配对连接,并不弹出提示框
我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。
在源码 BluetoothDevice
类中还有两个隐藏方法
cancelBondProcess()和cancelPairingUserInput()
这两个方法一个是取消配对进程一个是取消用户输入
下面是自动配对的代码
Mainfest,xml注册
&&/code&receiver
android:name=".BluetoothConnectActivityReceiver" &
&&&&&&/code&intent-filter&
&&&&&&&&&&/code&action
android:name="android.bluetooth.device.action.PAIRING_REQUEST"
&&&&&/&/code&intent-filter&
&/&/code&receiver&
自己在收到广播时处理并将预先输入的密码设置进去
class BluetoothConnectActivityReceiver extends BroadcastReceiver
&&&&String
strPsw = "0";
&&&&@Override
&&&&public
void onReceive(Context context, Intent intent)
&&&&&&&&//
TODO Auto-generated method stub
&&&&&&&&if
(intent.getAction().equals(
&&&&&&&&&&&&&&&&"android.bluetooth.device.action.PAIRING_REQUEST"))
&&&&&&&&&&&&BluetoothDevice
btDevice = intent
&&&&&&&&&&&&&&&&&&&&.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
&&&&&&&&&&&&//
byte[] pinBytes =
BluetoothDevice.convertPinToBytes("1234");
&&&&&&&&&&&&//
device.setPin(pinBytes);
&&&&&&&&&&&&Log.i("tag11111",
&&&&&&&&&&&&try
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&ClsUtils.setPin(btDevice.getClass(),
btDevice, strPsw); //
手机和蓝牙采集器配对
&&&&&&&&&&&&&&&&ClsUtils.createBond(btDevice.getClass(),
btDevice);
&&&&&&&&&&&&&&&&ClsUtils.cancelPairingUserInput(btDevice.getClass(),
btDevice);
&&&&&&&&&&&&}
&&&&&&&&&&&&catch
(Exception e)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&//
TODO Auto-generated catch block
&&&&&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&&&&&}
java.lang.reflect.F
java.lang.reflect.M
android.bluetooth.BluetoothD
android.util.L
class ClsUtils
&&&&static
public boolean createBond(Class
btClass, BluetoothDevice btDevice)
&&&&&&&&&&&&throws
&&&&&&&&Method
createBondMethod = btClass.getMethod("createBond");
&&&&&&&&Boolean
returnValue = (Boolean)
createBondMethod.invoke(btDevice);
&&&&&&&&return
returnValue.booleanValue();
&&&&static
public boolean removeBond(Class
btClass, BluetoothDevice btDevice)
&&&&&&&&&&&&throws
&&&&&&&&Method
removeBondMethod = btClass.getMethod("removeBond");
&&&&&&&&Boolean
returnValue = (Boolean)
removeBondMethod.invoke(btDevice);
&&&&&&&&return
returnValue.booleanValue();
&&&&static
public boolean setPin(Class btClass,
BluetoothDevice btDevice,
&&&&&&&&&&&&String
str) throws Exception
&&&&&&&&try
&&&&&&&&&&&&Method
removeBondMethod = btClass.getDeclaredMethod("setPin",
&&&&&&&&&&&&&&&&&&&&new
&&&&&&&&&&&&&&&&&&&&{byte[].class});
&&&&&&&&&&&&Boolean
returnValue = (Boolean)
removeBondMethod.invoke(btDevice,
&&&&&&&&&&&&&&&&&&&&new
&&&&&&&&&&&&&&&&&&&&{str.getBytes()});
&&&&&&&&&&&&Log.e("returnValue",
returnValue);
&&&&&&&&catch
(SecurityException e)
&&&&&&&&&&&&//
throw new RuntimeException(e.getMessage());
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&catch
(IllegalArgumentException e)
&&&&&&&&&&&&//
throw new RuntimeException(e.getMessage());
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&catch
(Exception e)
&&&&&&&&&&&&//
TODO Auto-generated catch block
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&return
取消用户输入
&&&&static
public boolean cancelPairingUserInput(Class btClass,
&&&&&&&&&&&&BluetoothDevice
&&&&throws
&&&&&&&&Method
createBondMethod = btClass.getMethod("cancelPairingUserInput");
&&&&&&&&//
cancelBondProcess()
&&&&&&&&Boolean
returnValue = (Boolean)
createBondMethod.invoke(device);
&&&&&&&&return
returnValue.booleanValue();
&&&&static
public boolean cancelBondProcess(Class btClass,
&&&&&&&&&&&&BluetoothDevice
&&&&throws
&&&&&&&&Method
createBondMethod = btClass.getMethod("cancelBondProcess");
&&&&&&&&Boolean
returnValue = (Boolean)
createBondMethod.invoke(device);
&&&&&&&&return
returnValue.booleanValue();
&&&&static
public void printAllInform(Class
&&&&&&&&try
&&&&&&&&&&&&//
取得所有方法
&&&&&&&&&&&&Method[]
hideMethod = clsShow.getMethods();
&&&&&&&&&&&&int
&&&&&&&&&&&&for
(; i & hideMethod. i++)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&Log.e("method
name", hideMethod[i].getName() +
";and the i is:"
&&&&&&&&&&&&&&&&&&&&&&&&+
&&&&&&&&&&&&}
&&&&&&&&&&&&//
取得所有常量
&&&&&&&&&&&&Field[]
allFields = clsShow.getFields();
&&&&&&&&&&&&for
(i = 0; i & allFields.
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&Log.e("Field
allFields[i].getName());
&&&&&&&&&&&&}
&&&&&&&&catch
(SecurityException e)
&&&&&&&&&&&&//
throw new RuntimeException(e.getMessage());
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&catch
(IllegalArgumentException e)
&&&&&&&&&&&&//
throw new RuntimeException(e.getMessage());
&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&catch
(Exception e)
&&&&&&&&&&&&//
TODO Auto-generated catch block
&&&&&&&&&&&&e.printStackTrace();
&执行时直接使用:
public static
boolean pair(String strAddr, String strPsw)
&&&&&&&&boolean
result = false;
&&&&&&&&BluetoothAdapter
bluetoothAdapter = BluetoothAdapter
&&&&&&&&&&&&&&&&.getDefaultAdapter();
&&&&&&&&bluetoothAdapter.cancelDiscovery();
&&&&&&&&if
(!bluetoothAdapter.isEnabled())
&&&&&&&&&&&&bluetoothAdapter.enable();
&&&&&&&&if
(!BluetoothAdapter.checkBluetoothAddress(strAddr))
// 检查蓝牙地址是否有效
&&&&&&&&&&&&Log.d("mylog",
"devAdd un effient!");
&&&&&&&&BluetoothDevice
device = bluetoothAdapter.getRemoteDevice(strAddr);
&&&&&&&&if
(device.getBondState() !=
BluetoothDevice.BOND_BONDED)
&&&&&&&&&&&&try
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&Log.d("mylog",
"NOT BOND_BONDED");
&&&&&&&&&&&&&&&&ClsUtils.setPin(device.getClass(),
device, strPsw); //
手机和蓝牙采集器配对
&&&&&&&&&&&&&&&&ClsUtils.createBond(device.getClass(),
&&&&&&&&&&&&&&&&remoteDevice
配对完毕就把这个设备对象传给全局的remoteDevice
&&&&&&&&&&&&&&&&result
&&&&&&&&&&&&}
&&&&&&&&&&&&catch
(Exception e)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&//
TODO Auto-generated catch block
&&&&&&&&&&&&&&&&Log.d("mylog",
"setPiN failed!");
&&&&&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&&&&&}
&&&&&&&&else
&&&&&&&&&&&&Log.d("mylog",
"HAS BOND_BONDED");
&&&&&&&&&&&&try
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&ClsUtils.createBond(device.getClass(),
&&&&&&&&&&&&&&&&ClsUtils.setPin(device.getClass(),
device, strPsw); //
手机和蓝牙采集器配对
&&&&&&&&&&&&&&&&ClsUtils.createBond(device.getClass(),
&&&&&&&&&&&&&&&&remoteDevice
如果绑定成功,就直接把这个设备对象传给全局的remoteDevice
&&&&&&&&&&&&&&&&result
&&&&&&&&&&&&}
&&&&&&&&&&&&catch
(Exception e)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&//
TODO Auto-generated catch block
&&&&&&&&&&&&&&&&Log.d("mylog",
"setPiN failed!");
&&&&&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&&&&&}
&&&&&&&&return
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 win10 蓝牙配对码无效 的文章

 

随机推荐