Android開發(fā)中編寫藍牙相關(guān)功能的核心代碼講解
一. 什么是藍牙(Bluetooth)?
1.1 BuleTooth是目前使用最廣泛的無線通信協(xié)議
1.2 主要針對短距離設(shè)備通訊(10m)
1.3 常用于連接耳機,鼠標和移動通訊設(shè)備等.
二. 與藍牙相關(guān)的API
2.1 BluetoothAdapter:
代表了本地的藍牙適配器
2.2 BluetoothDevice
代表了一個遠程的Bluetooth設(shè)備
三. 掃描已經(jīng)配對的藍牙設(shè)備(1)
注:必須部署在真實手機上,模擬器無法實現(xiàn)
首先需要在AndroidManifest.xml 聲明藍牙權(quán)限
<user-permission android:name="android.permission.BLUETOOTH" />
配對藍牙需要手動操作:
1. 打開設(shè)置--> 無線網(wǎng)絡 --> 藍牙 勾選開啟
2. 打開藍牙設(shè)置 掃描周圍已經(jīng)開啟的藍牙設(shè)備(可以與自己的筆記本電腦進行配對),點擊進行配對
電腦上會彈出提示窗口: 添加設(shè)備
顯示計算與設(shè)備之間的配對碼,要求確認是否配對
手機上也會顯示類似的提示.
四. 掃描已經(jīng)配對的藍牙設(shè)備(2)
4.1 獲得BluetoothAdapter對象
4.2 判斷當前移動設(shè)備中是否擁有藍牙
4.3 判斷當前移動設(shè)備中藍牙是否已經(jīng)打開
4.4 得到所有已經(jīng)配對的藍牙設(shè)備對象
藍牙配對實現(xiàn)的核心代碼如下:
MainActivity:
import java.util.Iterator;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//獲得BluetoothAdapter對象,該API是android 2.0開始支持的
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
//adapter不等于null,說明本機有藍牙設(shè)備
if(adapter != null){
System.out.println("本機有藍牙設(shè)備!");
//如果藍牙設(shè)備未開啟
if(!adapter.isEnabled()){
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//請求開啟藍牙設(shè)備
startActivity(intent);
}
//獲得已配對的遠程藍牙設(shè)備的集合
Set<BluetoothDevice> devices = adapter.getBondedDevices();
if(devices.size()>0){
for(Iterator<BluetoothDevice> it = devices.iterator();it.hasNext();){
BluetoothDevice device = (BluetoothDevice)it.next();
//打印出遠程藍牙設(shè)備的物理地址
System.out.println(device.getAddress());
}
}else{
System.out.println("還沒有已配對的遠程藍牙設(shè)備!");
}
}else{
System.out.println("本機沒有藍牙設(shè)備!");
}
}
});
}
}
修改本機藍牙設(shè)備的可見性,并掃描周圍可用的藍牙設(shè)備
1. 修改本機藍牙設(shè)備的可見性
2. 掃描周圍可用的藍牙設(shè)備
Eg:
一. 清單文件AdroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.se7en"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<!-若需要管理藍牙設(shè)備,如修改可見性,則需以下的權(quán)限->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</manifest>
二. 布局文件: main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/discoverButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="設(shè)置可見性"/>
<Button
android:id="@+id/scanButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="開始掃描"/>
</LinearLayout>
三. MainActivity:
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button discoverButton = null;
private Button scanButton = null;
private BluetoothAdapter adapter = null;
private BluetoothReceiver bluetoothReceiver = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = BluetoothAdapter.getDefaultAdapter();
discoverButton = (Button)findViewById(R.id.discoverButton);
scanButton = (Button)findViewById(R.id.scanButton);
//修改藍牙設(shè)備的可見性
discoverButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//設(shè)置藍牙可見性,500表示可見時間(單位:秒),當值大于300時默認為300
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);
startActivity(discoverIntent);
}
});
scanButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//開始掃描周圍藍牙設(shè)備,該方法是異步調(diào)用并以廣播的機制返回,所以需要創(chuàng)建一個BroadcastReceiver來獲取信息
adapter.startDiscovery();
}
});
//設(shè)定廣播接收的filter
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//創(chuàng)建藍牙廣播信息的receiver
bluetoothReceiver = new BluetoothReceiver ();
//注冊廣播接收器
registerReceiver(bluetoothReceiver,intentFilter);
}
private class BluetoothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//獲得掃描到的遠程藍牙設(shè)備
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getAddress());
}
}
}
- Android藍牙開發(fā)深入解析
- android實現(xiàn)藍牙文件發(fā)送的實例代碼,支持多種機型
- Android Bluetooth藍牙技術(shù)使用流程詳解
- Android編程之藍牙測試實例
- Android單片機與藍牙模塊通信實例代碼
- Android提高之藍牙傳感應用實例
- Android系統(tǒng)中的藍牙連接程序編寫實例教程
- Android 藍牙2.0的使用方法詳解
- Android藍牙通信聊天實現(xiàn)發(fā)送和接受功能
- Android藍牙通信編程
- Android設(shè)備間實現(xiàn)藍牙(Bluetooth)共享上網(wǎng)
- Android 藍牙開發(fā)實例解析
- Android實現(xiàn)的簡單藍牙程序示例
相關(guān)文章
Android EditText實現(xiàn)輸入金額類型詳解
EditText是Android中一個非常實用的控件,有很多InputType,可以來達到不同的輸入效果,下面這篇文章主要給大家介紹了關(guān)于Android EditText實現(xiàn)輸入金額類型的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
Android RecyclerView添加頭部和底部的方法
這篇文章主要為大家詳細介紹了Android RecyclerView添加頭部和底部的方法,感興趣的小伙伴們可以參考一下2016-05-05
Android報錯Didn‘t?find?class?“android.view.x“問題解決原理剖析
這篇文章主要為大家介紹了Android報錯Didn‘t?find?class?“android.view.x“問題解決及原理剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
Flutter實現(xiàn)底部導航欄創(chuàng)建詳解
ConvexBottomBar是一個底部導航欄組件,用于展現(xiàn)凸起的TAB效果,支持多種內(nèi)置樣式與動畫交互。本文將利用ConvexBottomBar創(chuàng)建漂亮的底部導航欄,感興趣的可以學習一下2022-01-01
Android實現(xiàn)院系專業(yè)三級聯(lián)動
這篇文章主要為大家詳細介紹了Android實現(xiàn)院系專業(yè)三級聯(lián)動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03
android中ProgressDialog與ProgressBar的使用詳解
本篇文章是對android中ProgressDialog與ProgressBar的使用進行了詳細的分析介紹,需要的朋友參考下2013-06-06
Android 實現(xiàn)兩個Activity跳轉(zhuǎn)實例
本文主要介紹Android 多個Activity相互之間的跳轉(zhuǎn),認識Activity生命周期,在做Android編程的時候用處很大,希望能幫助有需要的小伙伴2016-07-07

