當前位置:首頁 » 安卓系統 » android43藍牙40

android43藍牙40

發布時間: 2022-12-07 10:26:50

Ⅰ android中藍牙2.0和4.0的區別是什麼

最主要的區別就是藍牙2.0的傳輸速度沒有藍牙4.0快。以下為藍牙各版本的說明。x0dx0ax0dx0a1.1 為最早期版本,傳輸率約在748~810kb/s,因是早期設計,容易受到同頻率之產品所干擾下影響通訊質量。x0dx0a藍牙1.2標准x0dx0a1.2 同樣是只有 748~810kb/s 的傳輸率,但在加上了(改善 Software)抗干擾跳頻功能。x0dx0a藍牙2.0標准x0dx0a2.0 是 1.2 的改良提升版,傳輸率約在 1.8M/s~2.1M/s,開始支持雙工模式——即一面作語音通訊,同時亦可以傳輸檔案/高質素圖片,2.0 版本當然也支持 Stereo 運作。x0dx0a應用最為廣泛的是Bluetooth 2.0+EDR標准,該標准在2004年已經推出,支持Bluetooth 2.0+EDR標準的產品也於2006年大量出現。x0dx0a雖然Bluetooth 2.0+EDR標准在技術上作了大量的改進,但從1.X標准延續下來的配置流程復雜和設備功耗較大的問題依然存在。x0dx0a藍牙2.1標准x0dx0a2007年8月2日,藍牙技術聯盟今天正式批准了藍牙2.1版規范,即「藍牙2.1+EDR」,可供未來的設備自由使用。和2.0版本同時代產品,目前仍然占據藍牙市場較大份額,相對2.0版本主要是提高了待機時間2倍以上,技術標准沒有根本性變化。x0dx0a藍牙3.0標准x0dx0a2009年4月21日,藍牙技術聯盟(Bluetooth SIG)正式頒布了新一代標准規范"Bluetooth Core Specification Version 3.0 High Speed"(藍牙核心規范3.0版 ),藍牙3.0的核心是"Generic Alternate MAC/PHY"(AMP),這是一種全新的交替射頻技術,允許藍牙協議棧針對任一任務動態地選擇正確射頻。x0dx0a藍牙3.0的數據傳輸率提高到了大約24Mbps(即可在需要的時候調用802.11 WI-FI用於實現高速數據傳輸)。在傳輸速度上,藍牙3.0是藍牙2.0的八倍,可以輕松用於錄像機至高清電視、PC至PMP、UMPC至列印機之間的資料傳輸,但是需要雙方都達到此標准才能實現功能。x0dx0a藍牙4.0標准x0dx0a藍牙4.0規范於2010年7月7日正式發布,新版本的最大意義在於低功耗,同時加強不同OEM廠商之間的設備兼容性,並且降低延遲,理論最高傳輸速度依然為24Mbps(即3MB/s),有效覆蓋范圍擴大到100米(之前的版本為10米)。該標准晶元被大量的手機、平板所採用,如蘋果The New iPad平板電腦,以及蘋果iPhone 5、魅族MX4、HTC One X等手機上帶有藍牙4.0功能。x0dx0a藍牙4.1標准x0dx0a藍牙4.1於2013年12月6日發布,與LTE無線電信號之間如果同時傳輸數據,那麼藍牙4.1可以自動協調兩者的傳輸信息,理論上可以減少 其它信號對藍牙4.1的干擾。改進是提升了連接速度並且更加智能化,比如減少了設備之間重新連接的時間,意味著用戶如果走出了藍牙4.1的信號范圍並且斷開連接的時間不算很長,當用戶再次回到信號范圍中之後設備將自動連接,反應時間要比藍牙4.0更短。最後一個改進之處是提高傳輸效率,如果用戶連接的設備 非常多,比如連接了多部可穿戴設備,彼此之間的信息都能即時發送到接接收設備上。x0dx0a除此之外,藍牙4.1也為開發人員增加了更多的靈活性,這個改變對普通用戶沒有很大影響,但是對於軟體開發者來說是很重要的,因為為了應對逐漸興起的可穿戴設備,那麼藍牙必須能夠支持同時連接多部設備。x0dx0a目前支持該標準的手機還比較少,三星GALAXY Note4則是其中具有代表性的一款。x0dx0a藍牙4.2標准x0dx0a2014年12月4日,最新的藍牙4.2標准頒布,改善了數據傳輸速度和隱私保護程度,並接入了該設備將可直接通過IPv6和6LoWPAN接入互聯網。在新的標准下藍牙信號想要連接或者追蹤用戶設備必須經過用戶許可,否則藍牙信號將無法連接和追蹤用戶設備。x0dx0a速度方面變得更加快速,兩部藍牙設備之間的數據傳輸速度提高了2.5倍,因為藍牙智能(Bluetooth Smart)數據包的容量提高,其可容納的數據量相當於此前的10倍左右。藍牙的版本自然是越高級越好,考慮到傳輸距離和功耗的問題,最新的藍牙4.1是優選,但是目前市場上藍牙4.1的產品並不多,而主流的藍牙4.0產品性價比更高,至於藍牙3.0、2.1及以下的版本已經失去選購的價值。

Ⅱ Android手機藍牙不能用怎麼辦

Android手機藍牙不能用,可重啟一次手機,藍牙硬體會在重啟過程中會自動被重置。重啟後藍牙功能就可以正常啟動了。

Ⅲ 藍牙錯誤代碼43,怎麼解決

原因:

1、驅動沒有安裝正確,(安裝了不匹配的驅動文件,沒有按照程序要求完成驅動安裝過程,意外重啟);

2、內存沖突(這是由於安裝的驅動的兼容性導致的);

3、操作系統文件損壞;

4、硬體故障。

解決方法:

1、如果是驅動故障,需要卸載驅動程序,刪除驅動文件,否則重復安裝會無效。

2、重新安裝操作系統,使用計算機自帶的驅動光碟(這個首先要看是否支持WIN7),沒有光碟,到對應的官網下載即可。重新安裝驅動程序和驅動文件。按照安裝要求,重啟計算機。

(3)android43藍牙40擴展閱讀

藍牙各種版本的區別

藍牙1.0 :傳輸速率約1M/s。

藍牙2.0+EDR:傳輸速率約2-3M/s,其中2.1+EDR是最經典的藍牙,最大的特點是安全簡易配對。

藍牙3.0+HS(高傳輸藍牙):高傳輸24M/s,只有標注了"+HS"商標的設備才是真正支持802.11高速數據傳輸。

藍牙4.0(低功耗藍牙):它包括經典藍牙、高速藍牙和藍牙低功耗協議,在3.0基礎上功耗更低,主要面向對功耗需求極低、用紐扣電池供電的應用。其中4.1增加了物聯網特性,支持批量數據交換率共存,4.2的最大特性是可以讓多個藍牙智能設備通過一個終端接入區域網或互聯網。

Ⅳ Android開發之藍牙(Bluetooth)

在上一篇中有介紹了Wifi與網路連接處理
Android開發之WiFi與網路連接處理
下面,來繼續說說Android中藍牙的基本使用。

Bluetooth是目前使用的最廣泛的無線通訊協議之一,主要針對短距離設備通訊(10米),常用於連接耳機、滑鼠和移動通訊設備等。

值得一提的是:
android4.2新增了部分新功能,但是對於Bluetooth熟悉的人或許開始頭疼了,那就是Android4.2引入了一個新的藍牙協議棧針BLE。谷歌和Broadcom之間的合作,開發新的藍牙協議棧,取代了基於堆棧的Bluez。因此市場上出現了老設備的兼容問題,很多藍牙設備在android4.2手機上不能正常使用。

BluetoothAdapter簡單點來說就是代表了本設備(手機、電腦等)的藍牙適配器對象。

first:we need permission
要操作藍牙,先要在AndroidManifest.xml里加入許可權

**下面來看看如何使用藍牙。 **↓↓↓****
Demo已就緒:

返回值:如果設備具備藍牙功能,返回BluetoothAdapter 實例;否則,返回null對象。

打開藍牙設備的方式:
1.直接調用函數enable()去打開藍牙設備 ;
2.系統API去打開藍牙設備,該方式會彈出一個對話框樣式的Activity供用戶選擇是否打開藍牙設備。

注意: 1.如果藍牙已經開啟,不會彈出該Activity界面。2.在目前大多數Android手機中,是不支持在飛行模式下開啟藍牙的。如果藍牙已經開啟,那麼藍牙的開關 ,狀態會隨著飛行模式的狀態而發生改變。

1. 搜索藍牙設備
使用BluetoothAdapter的startDiscovery()方法來搜索藍牙設備
startDiscovery()方法是一個非同步方法,調用後會立即返回。該方法會進行對其他藍牙設備的搜索,該過程會持續12秒。該方法調用後,搜索過程實際上是在一個System Service中進行的,所以可以調用cancelDiscovery()方法來停止搜索(該方法可以在未執行discovery請求時調用)。

系統開始搜索藍牙設備
^( *  ̄(oo) ̄ ) ^ 系統會發送以下三個廣播:

2.掃描設備

3.定義廣播接收器接收搜索結果

4.注冊廣播

獲取附近的藍牙設備

第一步建立連接:首先Android sdk(2.0以上版本)支持的藍牙連接是通過BluetoothSocket建立連接,服務端BluetoothServerSocket和客戶端(BluetoothSocket)需指定同樣的UUID,才能建立連接,因為建立連接的方法會阻塞線程,所以伺服器端和客戶端都應啟動新線程連接。

(這里的服務端和客戶端是相對來說的)
兩個藍牙設備之間的連接,則必須實現服務端與客戶端的機制。
當兩個設備在同一個RFCOMM channel下分別擁有一個連接的BluetoothSocket,這兩個設備才可以說是建立了連接。

服務端設備與客戶端設備獲取BluetoothSocket的途徑是不同的。
1,服務端設備是通過accepted一個incoming connection來獲取的,
2,客戶端設備則是通過打開一個到服務端的RFCOMM channel來獲取的。

服務端
通過調用BluetoothAdapter的(String, UUID)方法來獲取BluetoothServerSocket(UUID用於客戶端與服務端之間的配對)

客戶端
調用BluetoothService的(UUID)方法獲取BluetoothSocket(該UUID應該同於服務端的UUID)。
調用BluetoothSocket的connect()方法(該方法為block方法),如果UUID同服務端的UUID匹配,並且連接被服務端accept,則connect()方法返回。

數據傳遞,通過以上操作,就已經建立的BluetoothSocket連接了,數據傳遞無非是通過流的形式
獲取流

該類就是關於遠程藍牙設備的一個描述。通過它可以和本地藍牙設備---BluetoothAdapter連接通信。

好多東西我也不知道怎麼描述,下面給出Demo:
剛好有剛學習的小夥伴問我ListView怎麼用,那我就用ListView。

源碼
RairDemo
GitHub: https://github.com/Rairmmd/android-demo
Coding: https://coding.net/u/Rair/p/RairDemo/git

Ⅳ Android中如何實現藍牙的配對與連接

藍牙功能可以參考下面的操作打開使用:
1.打開其他設備的藍牙,並使其對其他設備可見。
2.打開下拉頂簾,點擊藍牙圖標使其變為綠色,跳出提示框,勾選對其他設備可見。
3.點擊掃描,搜索到其他設備後,點擊該設備名稱,雙方點確定後配對成功。
4.選擇要傳輸的文件,共享通過藍牙即可傳輸文件。

Ⅵ 如何使用android原生BLE藍牙進行操作

之前的涉及的物聯網項目中使用的: BLE 低功耗藍牙(藍牙4.0), 支持android 4.3以上的手機
主從關系: BLE低功耗藍牙只能做從端設備 ,一個藍牙主端設備,可同時與7個藍牙從端設備進行通訊

1)低功耗
低功耗的原理:
1低功耗藍牙僅使用了3個廣播通道,傳統藍牙技術採用 16~32 個頻道
2每次廣播開啟時間也由傳統的 22.5ms 減少到 0.6~1.2ms(毫秒)

2)傳輸距離極大提高
傳統藍牙傳輸距離為 2~10m,而藍牙4.0的有效傳輸距離可達到 60~100m

3)安全性
使用AES-128 CCM加密演算法進行數據包加密和認證。
更多BLE藍牙的解析參考博客 : BLE4.0教程一 藍牙協議連接過程與廣播分析

添加許可權
打開藍牙
1.先拿到BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
2.再拿到BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
判斷是否打開藍牙
未打開彈出 系統彈框 ,除了 魅族手機 是打開系統設置

設備/手機都是藍牙信號

在回調方法中:

一般在掃描的過程中,我們還會設置 設備過濾原則 (因為我只想要搜索到我們想要的設備,忽略無關設備)
如:從 scanRecord -- beacon -- beacon.type == 0xFF代表Manufacture,通過與嵌入式軟體定義 自己的 Manufacture值即可

用BluetoothDevice得到BluetoothGatt:

斷連:

關鍵問題:連接後一般要做什麼事?

( 必須在剛連接成功後2秒內app寫一個值給設備,否則會被設備斷開連接)

主要是讀寫 characteristic
gatt.wirteCharacteristic(mCurrentcharacteristic);

gatt.readCharacteristic(characteristic);

bluetoothGatt.setCharacteristicNotification(data, true);

真實工作中使用的藍牙庫BlueToothKit請參考我的另一篇博客:
android藍牙入門知識和優秀藍牙第三方庫BluetoothKit的使用

Ⅶ 如何讓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 怎麼連接藍牙設備

先展示代碼結構
步驟閱讀
2
連接藍牙類
類名:MainActivity(有點偷懶,沒有起表意的類名。)
步驟閱讀
3
要聲明的控制項和變數等
步驟閱讀
4
在onCreate聲明控制項
步驟閱讀
5
ToogleButton設置開關狀態
聲明一個組件願意接收
IntentFilter intent = new IntentFilter();
步驟閱讀
6
BroadcastReceiver廣播接收器
步驟閱讀
步驟閱讀
7
listview點擊事件
OnItemClickListener

OnClickListener
步驟閱讀
步驟閱讀
8
藍牙連接
步驟閱讀
9
退出消耗頁面是的onDestroy()
步驟閱讀
10
布局結構圖
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.bluetooth_connection.MainActivity" >
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" >
<Button android:id="@+id/btnSearch" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="搜索" android:layout_weight="1" />
<Button android:id="@+id/btnExit" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="退出" android:layout_weight="1" />
<Button android:id="@+id/btnDis" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="開啟藍牙" android:layout_weight="1" /> </LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" >
<ToggleButton android:id="@+id/tbtnSwitch" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:textOff="關閉藍牙" android:textOn="開啟藍牙" /> </LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.3" android:orientation="vertical" >
<ListView android:id="@+id/lvDevices" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#f1f1f1" android:cacheColorHint="#ff333333" android:fadingEdge="none" android:scrollbars="none" > </ListView> </LinearLayout> </LinearLayout>
</RelativeLayout>

步驟閱讀
11
界面效果

步驟閱讀

12
例子如下

Ⅸ 如何實現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>

Ⅹ 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 的操作了。

熱點內容
安卓機哪裡看型號 發布:2024-03-29 15:59:40 瀏覽:280
cad解壓錯誤 發布:2024-03-29 15:01:45 瀏覽:78
存儲指令集 發布:2024-03-29 14:39:27 瀏覽:649
資料庫表刪除數據 發布:2024-03-29 14:39:26 瀏覽:367
出c語言整除 發布:2024-03-29 14:28:22 瀏覽:572
芬尼壓縮機 發布:2024-03-29 14:24:11 瀏覽:464
電腦數據實時上傳本地伺服器軟體 發布:2024-03-29 14:07:57 瀏覽:920
尋秦記源碼 發布:2024-03-29 13:56:17 瀏覽:496
linux的備份命令 發布:2024-03-29 13:41:22 瀏覽:383
csgo建議什麼配置 發布:2024-03-29 13:31:44 瀏覽:980