導航:首頁 > 操作系統 > android藍牙開發

android藍牙開發

發布時間:2022-01-12 14:36:36

1. 安卓開發 藍牙 後台

用一個service

java">//獲取適配器BluetoothAdaptermAdapter=BluetoothAdapter.getDefaultAdapter();
//不做提示,強行打開
if(!mAdapter.isEnabled()){
mAdapter.enable();
}
BroadcastReceivermReceiver=newBroadcastReceiver(){
publicvoidonReceive(Contextcontext,Intentintent){
Stringaction=intent.getAction();
//找到設備
if(BluetoothDevice.ACTION_FOUND.equals(action)){

BluetoothDevicedevice=intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(device.getBondState()!=BluetoothDevice.BOND_BONDED){


}

}

//搜索完成

elseif(BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)){
if(mNewDevicesAdapter.getCount()==0){

Log.v(TAG,"findover");

}

}
//執行更新列表的代碼後台這里就不需要更新
}
};

//注冊廣播接收者
IntentFilterfilter=newIntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver,filter);
filter=newIntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver,filter);
//這樣就可以獲取到所有的連接對象瞭然後在裡面選擇一個制定的mac地址使用
BluetoothSocketclienSocket=dcvice.(UUID);
clienSocket.connect();
inputStream=socket.getInputStream();
//將對象設備看作服務端然後get出流再將流轉化成字元串就可以判斷服務端發出的命令了這些都可以在service中完成純手打如有相同答案請看清楚時間先後杜絕抄襲!望樓主採納!~

2. android藍牙開發,PC端模擬串口接收字元,該如何編程

您好,android藍牙這方面還是很好搞的,因為大家的方式都是差不多的。先說說如何開啟藍牙設備和設置可見時間:

private void search() {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (!adapter.isEnabled()) {
adapter.enable();
}
Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600); //3600為藍牙設備可見時間
startActivity(enable);
Intent searchIntent = new Intent(this, ComminuteActivity.class);
startActivity(searchIntent);
}

首先,需要獲得一個BluetoothAdapter,可以通過getDefaultAdapter()獲得系統默認的藍牙適配器,當然我們也可以自己指定,但這個真心沒有必要,至少我是不需要的。然後我們檢查手機的藍牙是否打開,如果沒有,通過enable()方法打開。接著我們再設置手機藍牙設備的可見,可見時間可以自定義。

完成這些必要的設置後,我們就可以正式開始與藍牙模塊進行通信了:

public class ComminuteActivity extends Activity {
private BluetoothReceiver receiver;
private BluetoothAdapter bluetoothAdapter;
private List<String> devices;
private List<BluetoothDevice> deviceList;
private Bluetooth client;
private final String lockName = "BOLUTEK";
private String message = "000001";
private ListView listView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout);

listView = (ListView) this.findViewById(R.id.list);
deviceList = new ArrayList<BluetoothDevice>();
devices = new ArrayList<String>();
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
receiver = new BluetoothReceiver();
registerReceiver(receiver, filter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setContentView(R.layout.connect_layout);
BluetoothDevice device = deviceList.get(position);
client = new Bluetooth(device, handler);
try {
client.connect(message);
} catch (Exception e) {
Log.e("TAG", e.toString());
}
}
});
}

@Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}

private final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Bluetooth.CONNECT_FAILED:
Toast.makeText(ComminuteActivity.this, "連接失敗", Toast.LENGTH_LONG).show();
try {
client.connect(message);
} catch (Exception e) {
Log.e("TAG", e.toString());
}
break;
case Bluetooth.CONNECT_SUCCESS:
Toast.makeText(ComminuteActivity.this, "連接成功", Toast.LENGTH_LONG).show();
break;
case Bluetooth.READ_FAILED:
Toast.makeText(ComminuteActivity.this, "讀取失敗", Toast.LENGTH_LONG).show();
break;
case Bluetooth.WRITE_FAILED:
Toast.makeText(ComminuteActivity.this, "寫入失敗", Toast.LENGTH_LONG).show();
break;
case Bluetooth.DATA:
Toast.makeText(ComminuteActivity.this, msg.arg1 + "", Toast.LENGTH_LONG).show();
break;
}
}
};

private class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (isLock(device)) {
devices.add(device.getName());
}
deviceList.add(device);
}
showDevices();
}
}

private boolean isLock(BluetoothDevice device) {
boolean isLockName = (device.getName()).equals(lockName);
boolean isSingleDevice = devices.indexOf(device.getName()) == -1;
return isLockName && isSingleDevice;
}

private void showDevices() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
devices);
listView.setAdapter(adapter);
}
}

3. 如何使用Android藍牙開發

轉載 Android平台支持藍牙網路協議棧,實現藍牙設備之間數據的無線傳輸。本文檔描述了怎樣利用android平台提供的藍牙API去實現藍壓設備之間的通信。藍牙具有point-to-point 和 multipoint兩種連接功能。
使用藍牙API,可以做到:
* 搜索藍牙設備
* 從本地的Bluetooth adapter中查詢已經配對的設備
* 建立RFCOMM通道
* 通過service discovery連接到其它設備
* 在設備之間傳輸數據
* 管理多個連接

基礎知識
本文檔介紹了如何使用Android的藍牙API來完成的四個必要的主要任務,使用藍牙進行設備通信,主要包含四個部分:藍牙設置、搜索設備(配對的或可見的)、連接、傳輸數據。
所有的藍牙API在android.bluetooth包中。實現這些功能主要需要下面這幾個類和介面:

BluetoothAdapter
代表本地藍牙適配器(藍牙發射器),是所有藍牙交互的入口。通過它可以搜索其它藍牙設備,查詢已經配對的設備列表,通過已知的MAC地址創建BluetoothDevice,創建BluetoothServerSocket監聽來自其它設備的通信。

BluetoothDevice
代表了一個遠端的藍牙設備, 使用它請求遠端藍牙設備連接或者獲取 遠端藍牙設備的名稱、地址、種類和綁定狀態。 (其信息是封裝在 bluetoothsocket 中) 。

BluetoothSocket
代表了一個藍牙套接字的介面(類似於 tcp 中的套接字) ,他是應用程 序通過輸入、輸出流與其他藍牙設備通信的連接點。

BluetoothServerSocket
代表打開服務連接來監聽可能到來的連接請求 (屬於 server 端) , 為了連接兩個藍牙設備必須有一個設備作為伺服器打開一個服務套接字。 當遠端設備發起連 接連接請求的時候,並且已經連接到了的時候,Blueboothserversocket 類將會返回一個 bluetoothsocket。

BluetoothClass
描述了一個設備的特性(profile)或該設備上的藍牙大致可以提供哪些服務(service),但不可信。比如,設備是一個電話、計算機或手持設備;設備可以提供audio/telephony服務等。可以用它來進行一些UI上的提示。
BluetoothProfile

BluetoothHeadset
提供手機使用藍牙耳機的支持。這既包括藍牙耳機和免提(V1.5)模式。

BluetoothA2dp
定義高品質的音頻,可以從一個設備傳輸到另一個藍牙連接。 「A2DP的」代表高級音頻分配模式。

BluetoothHealth
代表了醫療設備配置代理控制的藍牙服務

BluetoothHealthCallback
一個抽象類,使用實現BluetoothHealth回調。你必須擴展這個類並實現回調方法接收更新應用程序的注冊狀態和藍牙通道狀態的變化。


代表一個應用程序的配置,藍牙醫療第三方應用注冊與遠程藍牙醫療設備交流。

BluetoothProfile.ServiceListener
當他們已經連接到或從服務斷開時通知BluetoothProfile IPX的客戶時一個介面(即運行一個特定的配置文件,內部服務)。

藍牙許可權
為了在你的應用中使用藍牙功能,至少要在AndroidManifest.xml中聲明兩個許可權:BLUETOOTH(任何藍牙相關API都要使用這個許可權) 和 BLUETOOTH_ADMIN(設備搜索、藍牙設置等)。

為了執行藍牙通信,例如連接請求,接收連接和傳送數據都必須有BLUETOOTH許可權。

必須要求BLUETOOTH_ADMIN的許可權來啟動設備發現或操縱藍牙設置。大多數應用程序都需要這個許可權能力,發現當地的藍牙設備。此許可權授予其他的能力不應該使用,除非應用程序是一個「電源管理」,將根據用戶要求修改的藍牙設置

注釋:要請求BLUETOOTH_ADMIN的話,必須要先有BLUETOOTH。

在你的應用manifest 文件中聲明藍牙許可權。例如:

<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>
通過查看<uses-permission>資料來聲明應用許可權獲取更多的信息。

藍牙設置
在你的應用通過藍牙進行通信之前,你需要確認設備是否支持藍牙,如果支持,確信它被打開。

如果不支持,則不能使用藍牙功能。如果支持藍牙,但不能夠使用,你剛要在你的應用中請求使用藍牙。這個要兩步完成,使用BluetoothAdapter。

1.獲取BluetoothAdapter

所有的藍牙活動請求BluetoothAdapter,為了獲取BluetoothAdapter,呼叫靜態方法getDefaultAdapter() 。這個會返回一個BluetoothAdapter,代表設備自己的藍牙適配器(藍牙無線電)。這個藍牙適配器應用於整個系統中,你的應用可以通過這個對象進行交互。如果getDefaultAdapter()返回null,則這個設備不支持藍牙。例如:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
2.打開藍牙

其次。你需要確定藍牙能夠使用。通過isEnabled()來檢查藍牙當前是否可用。如果這個方法返回false,則藍牙不能夠使用。為了請求藍牙使用,呼叫startActivityForResult()與的ACTION_REQUEST_ENABLE動作意圖。通過系統設置中啟用藍牙將發出一個請求(不停止藍牙應用)。例如:

if (mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
![http://developer.android.com/images/bt_enable_request.png]

對話框中顯示請求使用藍牙許可權。如果響應"Yes",這個進程完成(或失敗)後你的應用將能夠使用藍牙。
REQUEST_ENABLE_BT常量作為一個整型傳到startActivityForResult()中(值必須大於0),該系統傳回給你,在你onActivityResult()作為實現的requestCode參數。

如果調用藍牙成功,你的Activity就會在onActivityResult()中收到RESULT_OK結果,如果藍牙不能使用由於錯誤(或用戶響應「NO」那麼結果返回RESULT_CANCELED。

除了通過onActivityResult(),還可以通過監聽ACTION_STATE_CHANGED這個broadcast Intent來知道藍牙狀態是否改變。這個Intent包含EXTRA_STATE,EXTRA_PREVIOUS_STATE兩個欄位,分別代表新舊狀態。可能的值是STATE_TURNING_ON, STATE_ON, STATE_TURNING_OFF, 還有STATE_OFF。

小貼: Enabling discoverability 將自動啟用藍牙。如果您計劃執行藍牙活動之前,始終使設備可發現,你可以跳過上面的步驟2。參閱enabling discoverability。

搜索設備
使用BluetoothAdapter可以通過設備搜索或查詢配對設備找到遠程Bluetooth設備。

Device discovery(設備搜索)是一個掃描搜索本地已使能Bluetooth設備並且從搜索到的設備請求一些信息的過程(有時候會收到類似「discovering」,「inquiring」或「scanning」)。但是,搜索到的本地Bluetooth設備只有在打開被發現功能後才會響應一個discovery請求,響應的信息包括設備名,類,唯一的MAC地址。發起搜尋的設備可以使用這些信息來初始化跟被發現的設備的連接。
一旦與遠程設備的第一次連接被建立,一個pairing請求就會自動提交給用戶。如果設備已配對,配對設備的基本信息(名稱,類,MAC地址)就被保存下來了,能夠使用Bluetooth API來讀取這些信息。使用已知的遠程設備的MAC地址,連接可以在任何時候初始化而不必先完成搜索(當然這是假設遠程設備是在可連接的空間范圍內)。

需要記住,配對和連接是兩個不同的概念:

配對意思是兩個設備相互意識到對方的存在,共享一個用來鑒別身份的鏈路鍵(link-key),能夠與對方建立一個加密的連接。

連接意思是兩個設備現在共享一個RFCOMM信道,能夠相互傳輸數據。

目前Android Bluetooth API's要求設備在建立RFCOMM信道前必須配對(配對是在使用Bluetooth API初始化一個加密連接時自動完成的)。

下面描述如何查詢已配對設備,搜索新設備。

注意:Android的電源設備默認是不能被發現的。用戶可以通過系統設置讓它在有限的時間內可以被發現,或者可以在應用程序中要求用戶使能被發現功能。

查找匹配設備
在搜索設備前,查詢配對設備看需要的設備是否已經是已經存在是很值得的,可以調用getBondedDevices()來做到,該函數會返回一個描述配對設備BluetoothDevice的結果集。例如,可以使用ArrayAdapter查詢所有配對設備然後顯示所有設備名給用戶:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "n" + device.getAddress());
}
};
BluetoothDevice對象中需要用來初始化一個連接唯一需要用到的信息就是MAC地址。

4. 如何使用Android藍牙開發

Android平台支持藍牙網路協議棧,實現藍牙設備之間數據的無線傳輸。本文檔描述了怎樣利用android平台提供的藍牙API去實現藍壓設備之間的通信。藍牙具有point-to-point 和 multipoint兩種連接功能。
使用藍牙API,可以做到:
* 搜索藍牙設備
* 從本地的Bluetooth adapter中查詢已經配對的設備
* 建立RFCOMM通道
* 通過service discovery連接到其它設備
* 在設備之間傳輸數據
* 管理多個連接

基礎知識
本文檔介紹了如何使用Android的藍牙API來完成的四個必要的主要任務,使用藍牙進行設備通信,主要包含四個部分:藍牙設置、搜索設備(配對的或可見的)、連接、傳輸數據。
所有的藍牙API在android.bluetooth包中。實現這些功能主要需要下面這幾個類和介面:

BluetoothAdapter
代表本地藍牙適配器(藍牙發射器),是所有藍牙交互的入口。通過它可以搜索其它藍牙設備,查詢已經配對的設備列表,通過已知的MAC地址創建BluetoothDevice,創建BluetoothServerSocket監聽來自其它設備的通信。

BluetoothDevice
代表了一個遠端的藍牙設備, 使用它請求遠端藍牙設備連接或者獲取 遠端藍牙設備的名稱、地址、種類和綁定狀態。 (其信息是封裝在 bluetoothsocket 中) 。

BluetoothSocket
代表了一個藍牙套接字的介面(類似於 tcp 中的套接字) ,他是應用程 序通過輸入、輸出流與其他藍牙設備通信的連接點。

BluetoothServerSocket
代表打開服務連接來監聽可能到來的連接請求 (屬於 server 端) , 為了連接兩個藍牙設備必須有一個設備作為伺服器打開一個服務套接字。 當遠端設備發起連 接連接請求的時候,並且已經連接到了的時候,Blueboothserversocket 類將會返回一個 bluetoothsocket。

BluetoothClass
描述了一個設備的特性(profile)或該設備上的藍牙大致可以提供哪些服務(service),但不可信。比如,設備是一個電話、計算機或手持設備;設備可以提供audio/telephony服務等。可以用它來進行一些UI上的提示。
BluetoothProfile

BluetoothHeadset
提供手機使用藍牙耳機的支持。這既包括藍牙耳機和免提(V1.5)模式。

BluetoothA2dp
定義高品質的音頻,可以從一個設備傳輸到另一個藍牙連接。 「A2DP的」代表高級音頻分配模式。

BluetoothHealth
代表了醫療設備配置代理控制的藍牙服務

BluetoothHealthCallback
一個抽象類,使用實現BluetoothHealth回調。你必須擴展這個類並實現回調方法接收更新應用程序的注冊狀態和藍牙通道狀態的變化。

5. android 藍牙開發 ble 難嗎

還是比較難的,只有不斷學習了、
在Google I/O開發者年會上,Google宣布未來幾個月內其Android操作系統將全面支持Bluetooth Smart Ready和 Bluetooth Smart設備。
採用新版Android系統的行動電話和平板,只要搭載雙模藍牙晶元即屬於Bluetooth Smart Ready規格。Bluetooth Smart Ready是藍牙技術的進階標准,幾乎可與所有支持藍牙技術的電子產品兼容,包括鍵盤、耳機,以及超級省電的下一代Bluetooth Smart智能應用配件(Bluetooth Smart appcessory),如FitBit Flex智能腕帶及Pebble智能手錶等。

6. android 藍牙 開發問題

public boolean setName(String name)
Set the friendly Bluetooth name of the local Bluetoth adapter.
This name is visible to remote Bluetooth devices.
Valid Bluetooth names are a maximum of 248 UTF-8 characters, however many
remote devices can only display the first 40 characters, and some may be limited
to just 20.

文檔中不是有說明嗎!
最大有效值是用utf-8編碼的248個字元,然而有些藍牙設備最多能顯示40個字元,....20char。

7. Android藍牙開發怎麼檢查遠程藍牙設備是否打開

通過下面的代碼判斷藍牙是否鏈接:
BluetoothHeadset mBluetoothHeadset;

// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);

// Define Service Listener of BluetoothProfile
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
};

//call functions on mBluetoothHeadset to check if Bluetooth SCO audio is connected.
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
for ( final BluetoothDevice dev : devices ) {
return mBluetoothHeadset.isAudioConnected(dev);
}

// finally Close proxy connection after use.
mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset);

8. android 4.0 藍牙開發 怎麼入手

本文介紹Android ble 藍牙4.0,也就是說API level >= 18,且支持藍牙4.0的手機才可以使用,如果手機系統版本API level < 18,也是用不了藍牙4.0的哦。

首先發一下官方的demo,有興趣的可以過去看看:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html。android系統4.3以上,手機支持藍牙4.0,具有搜索,配對,連接,發現服務及特徵值,斷開連接等功能,下載地址:http://download.csdn.net/detail/lqw770737185/8116019。

一、了解api及概念

1.1 BluetoothGatt

繼承BluetoothProfile,通過BluetoothGatt可以連接設備(connect),發現服務(discoverServices),並把相應地屬性返回到BluetoothGattCallback

1.2 BluetoothGattCharacteristic

相當於一個數據類型,它包括一個value和0~n個value的描述(BluetoothGattDescriptor)

1.3 BluetoothGattDescriptor

描述符,對Characteristic的描述,包括范圍、計量單位等

1.4 BluetoothGattService

服務,Characteristic的集合。

1.5 BluetoothProfile

一個通用的規范,按照這個規范來收發數據。

1.6 BluetoothManager

通過BluetoothManager來獲取BluetoothAdapter

BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
1.7 BluetoothAdapter

一個Android系統只有一個BluetoothAdapter ,通過BluetoothManager 獲取

BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
1.8 BluetoothGattCallback

已經連接上設備,對設備的某些操作後返回的結果。這里必須提醒下,已經連接上設備後的才可以返回,沒有返回的認真看看有沒有連接上設備。

private BluetoothGattCallback GattCallback = new BluetoothGattCallback() {
// 這里有9個要實現的方法,看情況要實現那些,用到那些就實現那些
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){};
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){};
};
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
BluetoothGatt gatt = device.connectGatt(this, false, mGattCallback);
1.8.1:notification對應onCharacteristicChanged;

gatt.setCharacteristicNotification(characteristic, true);
1.8.2:readCharacteristic對應onCharacteristicRead;

gatt.readCharacteristic(characteristic);
1.8.3: writeCharacteristic對應onCharacteristicWrite;

gatt.wirteCharacteristic(mCurrentcharacteristic);
1.8.4:連接藍牙或者斷開藍牙 對應 onConnectionStateChange;

1.8.5: readDescriptor對應onDescriptorRead;

1.8.6:writeDescriptor對應onDescriptorWrite;

gatt.writeDescriptor(descriptor);
1.8.7:readRemoteRssi對應onReadRemoteRssi;

gatt.readRemoteRssi()
1.8.8:executeReliableWrite對應onReliableWriteCompleted;

1.8.9:discoverServices對應onServicesDiscovered。

gatt.discoverServices()
1.9 BluetoothDevice

掃描後發現可連接的設備,獲取已經連接的設備

二、開啟藍牙許可權

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
如果 android.hardware.bluetooth_le設置為false,可以安裝在不支持的設備上使用,判斷是否支持藍牙4.0用以下代碼就可以了

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "設備不支持藍牙4.0", Toast.LENGTH_SHORT).show();
finish();
}
三、對藍牙的啟動關閉操作

1、利用系統默認開啟藍牙對話框

if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
2、後台打開藍牙,不做任何提示,這個也可以用來自定義打開藍牙對話框啦

mBluetoothAdapter.enable();
3、後台關閉藍牙

mBluetoothAdapter.disable();
四、掃描設備,連接設備,獲取設備信息 ,斷開連接設備,自行查看官方demo,還是看demo比較清晰啊

9. Android藍牙開發代碼怎麼寫

開啟藍牙設備和設置可見時間:

privatevoidsearch(){
BluetoothAdapteradapter=BluetoothAdapter.getDefaultAdapter();
if(!adapter.isEnabled()){
adapter.enable();
}
Intentenable=newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,3600);//3600為藍牙設備可見時間
startActivity(enable);
IntentsearchIntent=newIntent(this,ComminuteActivity.class);
startActivity(searchIntent);
}

首先,需要獲得一個BluetoothAdapter,可以通過getDefaultAdapter()獲得系統默認的藍牙適配器,當然我們也可以自己指定,但這個真心沒有必要,至少我是不需要的。然後我們檢查手機的藍牙是否打開,如果沒有,通過enable()方法打開。接著我們再設置手機藍牙設備的可見,可見時間可以自定義。

http://www.cnblogs.com/wenjiang/p/3200138.html

閱讀全文

與android藍牙開發相關的資料

熱點內容
html輸出php變數 瀏覽:215
黃金金箔折解壓球 瀏覽:423
java正則表達式源碼 瀏覽:471
win執行python腳本 瀏覽:307
怎麼知道伺服器型號 瀏覽:984
宜家電動窗簾下載什麼app 瀏覽:129
程序員怎麼變革 瀏覽:862
ins亞洲伺服器地址 瀏覽:785
我們的孩子pdf 瀏覽:597
appstore軟體怎麼顯示屏幕 瀏覽:12
2950伺服器如何裝系統 瀏覽:884
豐田花冠壓縮比 瀏覽:501
我的世界如何買伺服器 瀏覽:457
進不去我的世界伺服器是怎麼回事 瀏覽:902
女程序員的世界 瀏覽:538
博圖fb源碼 瀏覽:694
查手機游戲伺服器ip地址 瀏覽:934
招商銀行app無法登錄怎麼回事 瀏覽:249
png格式轉換pdf格式 瀏覽:97
怎樣制壓縮包 瀏覽:601