㈠ 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
在源码 BluetoothDevice 类中还有两个隐藏方法 cancelBondProcess()和cancelPairingUserInput() 这两个方法一个是取消配对进程一个是取消用户输入 下面是自动配对的代码 Mainfest,xml注册 1 <</code>receiver android:name="." > 2 <</code>intent-filter> 3 <</code>action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> 4 </</code>intent-filter> 5 </</code>receiver> 自己在收到广播时处理并将预先输入的密码设置进去 01 public class extends BroadcastReceiver 02 { 03 04 String strPsw = "0"; 05 06 @Override 07 public void onReceive(Context context, Intent intent) 08 { 09 // TODO Auto-generated method stub 10 if (intent.getAction().equals( 11 "android.bluetooth.device.action.PAIRING_REQUEST")) 12 { 13 BluetoothDevice btDevice = intent 14 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 15 16 // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234"); 17 // device.setPin(pinBytes); 18 Log.i("tag11111", "ddd"); 19 try 20 { 21 ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对 22 ClsUtils.createBond(btDevice.getClass(), btDevice); 23 ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice); 24 } 25 catch (Exception e) 26 { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 } 31 32 33 } 34 } 001 002 import java.lang.reflect.Field; 003 import java.lang.reflect.Method; 004 005 import android.bluetooth.BluetoothDevice; 006 import android.util.Log; 007 public class ClsUtils 008 { 009 010 014 static public boolean createBond(Class btClass, BluetoothDevice btDevice) 015 throws Exception 016 { 017 Method createBondMethod = btClass.getMethod("createBond"); 018 Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); 019 return returnValue.booleanValue(); 020 } 021 022 026 static public boolean removeBond(Class btClass, BluetoothDevice btDevice) 027 throws Exception 028 { 029 Method removeBondMethod = btClass.getMethod("removeBond"); 030 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice); 031 return returnValue.booleanValue(); 032 } 033 034 static public boolean setPin(Class btClass, BluetoothDevice btDevice, 035 String str) throws Exception 036 { 037 try 038 { 039 Method removeBondMethod = btClass.getDeclaredMethod("setPin", 040 new Class[] 041 {byte[].class}); 042 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, 043 new Object[] 044 {str.getBytes()}); 045 Log.e("returnValue", "" + returnValue); 046 } 047 catch (SecurityException e) 048 { 049 // throw new RuntimeException(e.getMessage()); 050 e.printStackTrace(); 051 } 052 catch (IllegalArgumentException e) 053 { 054 // throw new RuntimeException(e.getMessage()); 055 e.printStackTrace(); 056 } 057 catch (Exception e) 058 { 059 // TODO Auto-generated catch block 060 e.printStackTrace(); 061 } 062 return true; 063 064 } 065 066 // 取消用户输入 067 static public boolean cancelPairingUserInput(Class btClass, 068 BluetoothDevice device) 069 070 throws Exception 071 { 072 Method createBondMethod = btClass.getMethod("cancelPairingUserInput"); 073 // cancelBondProcess() 074 Boolean returnValue = (Boolean) createBondMethod.invoke(device); 075 return returnValue.booleanValue(); 076 } 077 078 // 取消配对 079 static public boolean cancelBondProcess(Class btClass, 080 BluetoothDevice device) 081 082 throws Exception 083 { 084 Method createBondMethod = btClass.getMethod("cancelBondProcess"); 085 Boolean returnValue = (Boolean) createBondMethod.invoke(device); 086 return returnValue.booleanValue(); 087 } 088 089 093 static public void printAllInform(Class clsShow) 094 { 095 try 096 { 097 // 取得所有方法 098 Method[] hideMethod = clsShow.getMethods(); 099 int i = 0; 100 for (; i < hideMethod.length; i++) 101 { 102 Log.e("method name", hideMethod[i].getName() + ";and the i is:" 103 + i); 104 } 105 // 取得所有常量 106 Field[] allFields = clsShow.getFields(); 107 for (i = 0; i < allFields.length; i++) 108 { 109 Log.e("Field name", allFields[i].getName()); 110 } 111 } 112 catch (SecurityException e) 113 { 114 // throw new RuntimeException(e.getMessage()); 115 e.printStackTrace(); 116 } 117 catch (IllegalArgumentException e) 118 { 119 // throw new RuntimeException(e.getMessage()); 120 e.printStackTrace(); 121 } 122 catch (Exception e) 123 { 124 // TODO Auto-generated catch block 125 e.printStackTrace(); 126 } 127 } 128 } 执行时直接使用: 01 public static boolean pair(String strAddr, String strPsw) 02 { 03 boolean result = false; 04 BluetoothAdapter bluetoothAdapter = BluetoothAdapter 05 .getDefaultAdapter(); 06 07 bluetoothAdapter.cancelDiscovery(); 08 09 if (!bluetoothAdapter.isEnabled()) 10 { 11 bluetoothAdapter.enable(); 12 } 13 14 if (!BluetoothAdapter.checkBluetoothAddress(strAddr)) 15 { // 检查蓝牙地址是否有效 16 17 Log.d("mylog", "devAdd un effient!"); 18 } 19 20 BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr); 21 22 if (device.getBondState() != BluetoothDevice.BOND_BONDED) 23 { 24 try 25 { 26 Log.d("mylog", "NOT BOND_BONDED"); 27 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对 28 ClsUtils.createBond(device.getClass(), device); 29 remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDevice 30 result = true; 31 } 32 catch (Exception e) 33 { 34 // TODO Auto-generated catch block 35 36 Log.d("mylog", "setPiN failed!"); 37 e.printStackTrace(); 38 } // 39 40 } 41 else 42 { 43 Log.d("mylog", "HAS BOND_BONDED"); 44 try 45 { 46 ClsUtils.createBond(device.getClass(), device); 47 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对 48 ClsUtils.createBond(device.getClass(), device); 49 remoteDevice = device; // 如果绑定成功,就直接把这个设备对象传给全局的remoteDevice 50 result = true; 51 } 52 catch (Exception e) 53 { 54 // TODO Auto-generated catch block 55 Log.d("mylog", "setPiN failed!"); 56 e.printStackTrace(); 57 } 58 } 59 return result; 60 }
㈡ 求安卓Android手机上的的蓝牙串口通讯源码!!!!
http://code.eoe.cn/789?f_section=new
以上是地址 自己去下
㈢ android_studio手机蓝牙串口通信源代码
初涉android的蓝牙操作,按照固定MAC地址连接获取Device时,程序始终是异常终止,查了好多天代码都没查出原因。今天改了一下API版本,突然就成功连接了。总结之后发现果然是个坑爹之极的错误。
为了这种错误拼命查原因浪费大把时间是非常不值得的,但是问题不解决更是揪心。可惜我网络了那么多,都没有给出确切原因。今天特此mark,希望后来者遇到这个问题的时候能轻松解决。
下面是我的连接过程,中间崩溃原因及解决办法。
1:用AT指令获得蓝牙串口的MAC地址,地址是简写的,按照常理猜测可得标准格式。
2:开一个String adress= "************" //MAC地址, String MY_UUID= "************"//UUID根据通信而定,网上都有。
3:取得本地Adapter用getDefaultAdapter(); 远程的则用getRemoteDevice(adress); 之后便可用UUID开socket进行通信。
如果中途各种在getRemoteDevice处崩溃,大家可以查看一下当前的API版本,如果是2.1或以下版本的话,便能确定是API版本问题,只要换成2.2或者以上就都可以正常运行了~ 这么坑爹的错误的确很为难初学者。 唉·········· 为这种小trick浪费很多时间真是难过。
(另外有个重要地方,别忘了给manifest里面加以下两个蓝牙操作权限哦~)
<uses-permissionandroid:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
下面附上Android蓝牙操作中用固定MAC地址传输信息的模板,通用搜索模式日后再补删模板:
=null;
=null;
privateOutputStreamoutStream=null;
privateInputStreaminStream=null;
privatestaticfinalUUIDMY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//这条是蓝牙串口通用的UUID,不要更改
privatestaticStringaddress="00:12:02:22:06:61";//<==要连接的蓝牙设备MAC地址
/*获得通信线路过程*/
/*1:获取本地BlueToothAdapter*/
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null)
{
Toast.makeText(this,"Bluetoothisnotavailable.",Toast.LENGTH_LONG).show();
finish();
return;
}
if(!mBluetoothAdapter.isEnabled())
{
Toast.makeText(this,"-runthisprogram.",Toast.LENGTH_LONG).show();
finish();
return;
}
/*2:获取远程BlueToothDevice*/
BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);
if(mBluetoothAdapter==null)
{
Toast.makeText(this,"Can'tgetremotedevice.",Toast.LENGTH_LONG).show();
finish();
return;
}
/*3:获得Socket*/
try{
btSocket=device.(MY_UUID);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Socketcreationfailed.",e);
}
/*4:取消discovered节省资源*/
mBluetoothAdapter.cancelDiscovery();
/*5:连接*/
try{
btSocket.connect();
Log.e(TAG,"ONRESUME:BTconnectionestablished,datatransferlinkopen.");
}catch(IOExceptione){
try{
btSocket.close();
}catch(IOExceptione2){
Log.e(TAG,"ONRESUME:",e2);
}
}
/*此时可以通信了,放在任意函数中*/
/*try{
outStream=btSocket.getOutputStream();
inStream=btSocket.getInputStream();//可在TextView里显示
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
}
Stringmessage="1";
byte[]msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionringwrite.",e);
}
*/
通用搜索模式代码模板:
简洁简洁方式1 demo
作用: 用VerticalSeekBar控制一个 LED屏幕的亮暗。
直接上码咯~
packagecom.example.seed2;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.app.Dialog;
importandroid.os.Bundle;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.util.UUID;
importandroid.bluetooth.BluetoothAdapter;
importandroid.bluetooth.BluetoothDevice;
importandroid.bluetooth.BluetoothSocket;
importandroid.content.DialogInterface;
importandroid.util.Log;
importandroid.view.KeyEvent;
importandroid.widget.Toast;
{
privatestaticfinalStringTAG="BluetoothTest";
=null;
=null;
privateOutputStreamoutStream=null;
privateInputStreaminStream=null;
privateVerticalSeekBarvskb=null;
privatestaticfinalUUIDMY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//这条是蓝牙串口通用的UUID,不要更改
privatestaticStringaddress="00:12:02:22:06:61";//<==要连接的蓝牙设备MAC地址
/**.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.vskb=(VerticalSeekBar)super.findViewById(R.id.mskb);
this.vskb.setOnSeekBarChangeListener(newOnSeekBarChangeListenerX());
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null)
{
Toast.makeText(this,"Bluetoothisnotavailable.",Toast.LENGTH_LONG).show();
finish();
return;
}
if(!mBluetoothAdapter.isEnabled())
{
Toast.makeText(this,"-runthisprogram.",Toast.LENGTH_LONG).show();
finish();
return;
}
}
.OnSeekBarChangeListener{
publicvoidonProgressChanged(VerticalSeekBarseekBar,intprogress,booleanfromUser){
//Main.this.clue.setText(seekBar.getProgress());
/*Stringmessage;
byte[]msgBuffer;
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:OutputStreamcreationfailed.",e);
}
message=Integer.toString(seekBar.getProgress());
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionringwrite.",e);
}*/
}
(VerticalSeekBarseekBar){
Stringmessage;
byte[]msgBuffer;
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:OutputStreamcreationfailed.",e);
}
message=Integer.toString(seekBar.getProgress());
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionringwrite.",e);
}
}
publicvoidonStopTrackingTouch(VerticalSeekBarseekBar){
Stringmessage;
byte[]msgBuffer;
try{
outStream=btSocket.getOutputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:OutputStreamcreationfailed.",e);
}
message=Integer.toString(seekBar.getProgress());
msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionringwrite.",e);
}
}
}
@Override
publicvoidonStart()
{
super.onStart();
}
@Override
publicvoidonResume()
{
super.onResume();
BluetoothDevicedevice=mBluetoothAdapter.getRemoteDevice(address);
try{
btSocket=device.(MY_UUID);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Socketcreationfailed.",e);
}
mBluetoothAdapter.cancelDiscovery();
try{
btSocket.connect();
Log.e(TAG,"ONRESUME:BTconnectionestablished,datatransferlinkopen.");
}catch(IOExceptione){
try{
btSocket.close();
}catch(IOExceptione2){
Log.e(TAG,"ONRESUME:",e2);
}
}
//.
/*try{
outStream=btSocket.getOutputStream();
inStream=btSocket.getInputStream();
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Outputstreamcreationfailed.",e);
}
Stringmessage="read";
byte[]msgBuffer=message.getBytes();
try{
outStream.write(msgBuffer);
}catch(IOExceptione){
Log.e(TAG,"ONRESUME:Exceptionringwrite.",e);
}
intret=-1;
while(ret!=-1)
{
try{
ret=inStream.read();
}catch(IOExceptione)
{
e.printStackTrace();
}
}
*/
}
@Override
㈣ 谁有android蓝牙与电脑蓝牙之间的通信源码啊,或者只要一端装了apk也行。
官方sdk自带一个蓝牙例子,自己找吧,名字就叫bluetooth
㈤ 谁能给一份开发基于蓝牙4.0的android的demo源码
请参考如下WIKI页面,基本上目前有提供的BLE的参考代码都集中到这个页面了:http://processors.wiki.ti.com/index.php/Category:BluetoothLE
㈥ 怎么将手机中的应用软件用蓝牙分享到另一手机
1、以华为P10为例,在桌面选择需要分享的软件;
㈦ 手机端应用怎么和蓝牙连接
手机蓝牙常用于与蓝牙耳机连接,以免提接听拔打电话或听音乐。以下是连接步骤:
手机要先打开蓝牙并处在被发现状态,按住蓝牙耳机开关键不放,直到手机蓝牙搜索到耳机并显示出耳机型号,再点击手机中显示的耳机型号进行连接(有时还需要输入PIN码,默认一般是”1234"或“0000”),显示“已连接”说明连接成功,此时方可松开耳机开关键。
蓝牙连接的有效距离是10米以内,使用过程中一定保持在这个距离范围内,超出范围会暂时断开连接。
㈧ 关于android系统蓝牙都用什么模块,有单独的android驱动吗
Android系统的源码中蓝牙部分的分布式这样的:
1. App部分主要包括OPP和PBAP以及HFP,当然还有settings部分,代码分布如下:
(1) /packages/app/Bluetooth/src/com/android/bluetooth/opp
(2) /packages/app/Bluetooth/src/com/android/bluetooth/pbap
(3) /packages/app/Phone/src/com/android/phone/ 中和BT相关的部分,主要是HFHS打电话相关的
(4) /packages/app/Settings/src/com/android/settings/bluetooth/ 主要是蓝牙开启,搜索,配对等
2. Framework部分主要包括BluetoothAdapter,BluetoothService,BluetoothEventLoop,BluetoothA2dpService等核心类,代码分布如下:
(1)/frameworks/base/core/java/android/server/ 包含BluetoothService,BluetoothEventLoop等核心类,但是并没有向用户应用程序提供接口,属于系统的类。
(2)/frameworks/base/core/java/android/bluetooth 包含了BluetoothAdapter, BluetoothSocket, BluetoothServerSocket等一系列类,这些类是系统向应用程序提供的接口,编写蓝牙相关的应用程序时会用到这些类,检查BT是否打开,搜索设备等等。如果编写蓝牙应用程序,需要熟悉该部分类的功能。
(3) /frameworks/base/core/jni/ 中包含Bluetooth相关的文件,如 android_server_BluetoothService.cpp 该部分主要是java层和c/c++层进行通信的
3. Bluez部分,主要包括各个协议的实现,如A2dp,AVRCP,AVDTP,HID,HDP,PAN,DUN以及对BT host的实现,通过该部分向BT Controler部分发送HCI命令,代码分布服下:
/external/bluetooth/bluez
4. BluetDroid部分,主要用于开关蓝牙,芯片上电部分(不属于驱动,可以算是硬件抽象层(HAL)), 代码分布如下:
/system/bluetooth/
5. kernel部分,主要包括RFCOMM,L2CAP等协议以及HCI的实现,代码分布如下:
/kernel/net/bluetooth/
6. driver部分,该部分代码还真没了解过,代码分布:
/kernel/drivers/bluetooth/
另外,driver部分和具体芯片密切相关,有时在下面的目录下也会有:
/vendor/
/device/