android60藍牙
『壹』 Android開發 藍牙連接問題
Android 藍牙編程的基本步驟:
1.獲取藍牙適配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter(); 
如果BluetoothAdapter 為null,說明android手機沒有藍牙模塊。
判斷藍牙模塊是否開啟,blueadapter.isEnabled() true表示已經開啟,false表示藍牙並沒啟用。
2.啟動配置藍牙可見模式,即進入可配對模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);  
startActivity(in);  ,200就表示200秒。
3.獲取藍牙適配器中已經配對的設備Set<BluetoothDevice> device=blueadapter.getBondedDevices(); 
4.還需要在androidManifest.xml中聲明藍牙的許可權
 <uses-permission android:name="android.permission.BLUETOOTH" />
 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  
接下來就是根據自己的需求對BluetoothAdapter 的操作了。
『貳』 如何實現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 手機藍牙怎樣自動連接已配對的設備
藍牙和手機的詳細連接步驟如下:
1,手機打開藍牙功能,進去搜索藍牙耳機狀態。
2,使藍牙耳機在開機狀態,需要長按藍牙耳機的接聽鍵。
3,直到指示燈:藍燈紅燈交替閃爍再松開。
4,這個時候藍牙耳機和手機會再次進入匹對。
5,注意手機和藍牙距離要在10米內。
等手機上面顯示此藍牙型號設備後,點擊確定即可連接使用了。
『肆』 Android手機藍牙不能用怎麼辦
Android手機藍牙不能用,可重啟一次手機,藍牙硬體會在重啟過程中會自動被重置。重啟後藍牙功能就可以正常啟動了。
『伍』 如何實現android藍牙自動配對連接
之前做一個Android版的藍牙 與血壓計通訊的項目,遇到最大的難題就是自動配對.
上網查資料說是用反射createBond()和setPin(),但測試時進行配對還是會出現提示,但配對是成功了
我就開始查找怎麼關閉這個藍牙配對提示框,後面還是偉大的android源碼幫助了我。
在源碼 BluetoothDevice 類中還有兩個隱藏方法
cancelBondProcess()和cancelPairingUserInput()
這兩個方法一個是取消配對進程一個是取消用戶輸入
下面是自動配對的代碼
Mainfest,xml注冊
 
1    <receiver android:name="." >    
2        <intent-filter>    
3            <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />    
4        </intent-filter>    
5    </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    <b>/************************************ 藍牙配對函數 * **************/    
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        /**    
011         * 與設備配對 參考源碼:platform/packages/apps/Settings.git    
012         * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java    
013         */    
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        /**    
023         * 與設備解除配對 參考源碼:platform/packages/apps/Settings.git    
024         * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java    
025         */    
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        /**    
090         *    
091         * @param clsShow    
092         */    
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    }</b>    
 執行時直接使用:
 
view sourceprint?
01    <b>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        }</b>
