傳統(tǒng)藍(lán)牙適用于較為耗電的操作,其中包括 Android 設(shè)備之間的流式傳輸和通信等。針對(duì)具有低功耗要求的藍(lán)牙設(shè)備,Android 4.3(API 級(jí)別 18)中引入了面向低功耗藍(lán)牙的 API 支持。因?yàn)轫?xiàng)目需求,編寫了關(guān)于傳統(tǒng)藍(lán)牙的demo,實(shí)現(xiàn)了藍(lán)牙多連,藍(lán)牙間信息發(fā)送與接收,有不對(duì)的地方,望指導(dǎo),大家一起學(xué)習(xí),謝謝!
大概效果如下:
go!go!go!
首先權(quán)限問(wèn)題
如要在應(yīng)用中使用藍(lán)牙功能,您必須聲明兩個(gè)權(quán)限。第一個(gè)是?BLUETOOTH。您需要此權(quán)限才能執(zhí)行任何藍(lán)牙通信,例如請(qǐng)求連接、接受連接和傳輸數(shù)據(jù)等。
第二個(gè)必須聲明的權(quán)限是?ACCESS_FINE_LOCATION。您的應(yīng)用需要此權(quán)限,因?yàn)樗{(lán)牙掃描可用于收集用戶的位置信息。此類信息可能來(lái)自用戶自己的設(shè)備,以及在商店和交通設(shè)施等位置使用的藍(lán)牙信標(biāo)。
如果您想讓應(yīng)用啟動(dòng)設(shè)備發(fā)現(xiàn)或操縱藍(lán)牙設(shè)置,則除了?BLUETOOTH?權(quán)限以外,您還必須聲明?BLUETOOTH_ADMIN?權(quán)限。大多數(shù)應(yīng)用只是需利用此權(quán)限發(fā)現(xiàn)本地藍(lán)牙設(shè)備。除非應(yīng)用是根據(jù)用戶請(qǐng)求修改藍(lán)牙設(shè)置的“超級(jí)管理員”,否則不應(yīng)使用此權(quán)限所授予的其他功能。
在您的應(yīng)用清單文件中聲明藍(lán)牙權(quán)限。例如:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
1.藍(lán)牙幫助類 BlueToothUtils
public class BlueToothUtils implements BlueToothUtilsBroadcastReceiver.BlueToothUtilsBroadcastReceiverListener {
private static BlueToothUtilsblueToothUtils;
? ? public synchronized static BlueToothUtilsgetInstance(Activity activity) {
if (null ==blueToothUtils) {
blueToothUtils =new BlueToothUtils(activity);
? ? ? ? }
return blueToothUtils;
? ? }
private BluetoothAdapterbluetoothAdapter;
? ? private BluetoothServerSocketmmServerSocket;
? ? private Threadthread;
? ? private boolean isRunning =false;
? ? private final Stringlock ="lock";
? ? private final Stringlock2 ="lock2";
? ? private BlueToothUtilsBroadcastReceiverblueToothUtilsBroadcastReceiver;
? ? private MapstringBluetoothSocketMap;
? ? private BlueToothUtilsListenerlistener;
? ? private StringIMEI;
? ? private Activitycontext;
? ? public BlueToothUtils(Activity context) {
this.context = context;
? ? ? ? bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
? ? ? ? stringBluetoothSocketMap =new HashMap<>();
? ? ? ? IMEI =bluetoothAdapter.getName();
? ? ? ? registerBlueStateReceiver();
? ? ? ? if (null !=bluetoothAdapter) {
try {
mmServerSocket =bluetoothAdapter.listenUsingRfcommWithServiceRecord(IMEI, UUID.nameUUIDFromBytes(IMEI.getBytes()));
? ? ? ? ? ? }catch (IOException e) {
e.printStackTrace();
? ? ? ? ? ? }
}
}
private void registerBlueStateReceiver() {
blueToothUtilsBroadcastReceiver =new BlueToothUtilsBroadcastReceiver();
? ? ? ? blueToothUtilsBroadcastReceiver.setOnBlueToothUtilsBroadcastReceiverListener(this);
? ? ? ? IntentFilter intentFilter =new IntentFilter();
? ? ? ? // 監(jiān)視藍(lán)牙關(guān)閉和打開的狀態(tài)
? ? ? ? intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
? ? ? ? // 監(jiān)視藍(lán)牙設(shè)備與APP連接的狀態(tài)
? ? ? ? intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
? ? ? ? intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
? ? ? ? // 注冊(cè)廣播
? ? ? ? context.registerReceiver(blueToothUtilsBroadcastReceiver, intentFilter);
? ? }
public void start() {
synchronized (lock) {
if (null ==mmServerSocket) {
return;
? ? ? ? ? ? }
if (isRunning)return;
? ? ? ? ? ? isRunning =true;
? ? ? ? ? ? thread =new Thread(() -> {
while (isRunning) {
try {
BluetoothSocket socket =mmServerSocket.accept();
? ? ? ? ? ? ? ? ? ? ? ? BluetoothDevice remoteDevice = socket.getRemoteDevice();
? ? ? ? ? ? ? ? ? ? ? ? String name = remoteDevice.getName();
? ? ? ? ? ? ? ? ? ? ? ? addSocket(name, socket);
? ? ? ? ? ? ? ? ? ? ? ? receiveMessage(name);
? ? ? ? ? ? ? ? ? ? }catch (IOException e) {
e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
}
});
? ? ? ? ? ? thread.start();
? ? ? ? }
}
public void receiveMessage(String name) {
synchronized (lock2) {
if (null ==stringBluetoothSocketMap)return;
? ? ? ? ? ? if (!stringBluetoothSocketMap.containsKey(name))return;
? ? ? ? ? ? BlueToothEntity blueToothEntity =stringBluetoothSocketMap.get(name);
? ? ? ? ? ? if (null == blueToothEntity)return;
? ? ? ? ? ? if (!blueToothEntity.isRunning()) {
blueToothEntity.startListener();
? ? ? ? ? ? }
}
}
public void addSocket(String key, BluetoothSocket socket) {
synchronized (lock2) {
if (TextUtils.isEmpty(key) ||null == socket)return;
? ? ? ? ? ? if (null ==stringBluetoothSocketMap) {
stringBluetoothSocketMap =new HashMap<>();
? ? ? ? ? ? }
stringBluetoothSocketMap.put(key, new BlueToothEntity(socket, listener));
? ? ? ? }
}
public void sendMessage(String message) {
synchronized (lock2) {
if (null ==stringBluetoothSocketMap ||stringBluetoothSocketMap.size() ==0)return;
? ? ? ? ? ? Log.e("阿斯達(dá)所asdas0", "sendMessage");
? ? ? ? ? ? for (BlueToothEntity blueToothEntity :stringBluetoothSocketMap.values()) {
blueToothEntity.sendMessage(message);
? ? ? ? ? ? }
}
}
public void connect(BluetoothDevice device) {
MyThreadPool.getInstance().newThread(() -> {
try {
String name =device.getName().trim();
? ? ? ? ? ? ? ? BluetoothSocket tmp =device.createRfcommSocketToServiceRecord(UUID.nameUUIDFromBytes(name.getBytes()));
? ? ? ? ? ? ? ? tmp.connect();
? ? ? ? ? ? ? ? addSocket(name, tmp);
? ? ? ? ? ? ? ? receiveMessage(name);
? ? ? ? ? ? }catch (IOException e) {
e.printStackTrace();
? ? ? ? ? ? }
});
? ? }
public void setOnBlueToothUtilsListener(BlueToothUtilsListener listener) {
this.listener = listener;
? ? }
@Override
? ? public void onBlueToothClose(int blueToothSate, BluetoothDevice device) {
synchronized (lock2) {
if (null ==stringBluetoothSocketMap)return;
? ? ? ? ? ? stringBluetoothSocketMap.clear();
? ? ? ? }
}
@Override
? ? public void onBlueToothLink(String action, BluetoothDevice device) {
synchronized (lock2) {
if (null ==stringBluetoothSocketMap) {
stringBluetoothSocketMap =new HashMap<>();
? ? ? ? ? ? }
String name = device.getName();
? ? ? ? ? ? if (!stringBluetoothSocketMap.containsKey(name))return;
? ? ? ? ? ? BlueToothEntity blueToothEntity =stringBluetoothSocketMap.get(name);
? ? ? ? ? ? if (null != blueToothEntity&&!blueToothEntity.isRunning()) {
blueToothEntity.startListener();
? ? ? ? ? ? }
}
}
@Override
? ? public void onBlueToothLinkBreak(String action, BluetoothDevice device) {
synchronized (lock2) {
if (null ==stringBluetoothSocketMap)return;
? ? ? ? ? ? String name = device.getName();
? ? ? ? ? ? if (stringBluetoothSocketMap.containsKey(name)) {
BlueToothEntity blueToothEntity =stringBluetoothSocketMap.get(name);
? ? ? ? ? ? ? ? if (null != blueToothEntity) {
blueToothEntity.stopListener();
? ? ? ? ? ? ? ? ? ? stringBluetoothSocketMap.remove(name);
? ? ? ? ? ? ? ? }
}
}
}
public interface BlueToothUtilsListener {
void onBlueToothUtilsListener(String message, BluetoothSocket bluetoothSocket);
? ? }
public void onDestroy() {
context.unregisterReceiver(blueToothUtilsBroadcastReceiver);
? ? ? ? this.context =null;
? ? ? ? this.listener =null;
? ? ? ? this.isRunning =false;
? ? ? ? if (null !=thread) {
thread.interrupt();
? ? ? ? ? ? thread =null;
? ? ? ? }
if (null !=mmServerSocket) {
try {
mmServerSocket.close();
? ? ? ? ? ? }catch (IOException e) {
e.printStackTrace();
? ? ? ? ? ? }
mmServerSocket =null;
? ? ? ? }
bluetoothAdapter =null;
? ? ? ? synchronized (lock2) {
if (null !=stringBluetoothSocketMap) {
for (BlueToothEntity toothEntity :stringBluetoothSocketMap.values()) {
toothEntity.stopListener();
? ? ? ? ? ? ? ? }
stringBluetoothSocketMap.clear();
? ? ? ? ? ? ? ? stringBluetoothSocketMap =null;
? ? ? ? ? ? }
}
}
}
2.BlueToothUtils類里接收的BluetoothSocket去實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽
public class BlueToothEntity {
private InputStreamins =null;
? ? private OutputStreamops =null;
? ? private BluetoothSocketbluetoothSocket;
? ? private BlueToothUtils.BlueToothUtilsListenerlistener;
? ? private byte[]mmBuffer;
? ? private ThreadreceiveThread;
? ? private boolean isRunning =false;
? ? public BlueToothEntity(BluetoothSocket bluetoothSocket, BlueToothUtils.BlueToothUtilsListener listener) {
this.bluetoothSocket = bluetoothSocket;
? ? ? ? this.listener = listener;
? ? ? ? mmBuffer =new byte[1024];
? ? ? ? try {
ins =this.bluetoothSocket.getInputStream();
? ? ? ? ? ? ops =this.bluetoothSocket.getOutputStream();
? ? ? ? }catch (IOException e) {
e.printStackTrace();
? ? ? ? }
}
public boolean isRunning() {
return isRunning;
? ? }
public void startListener() {
if (null ==ins)return;
? ? ? ? if (this.isRunning)return;
? ? ? ? this.isRunning =true;
? ? ? ? this.receiveThread =new Thread(() -> {
while (this.isRunning) {
int numBytes =0;
? ? ? ? ? ? ? ? try {
this.mmBuffer =new byte[1024];
? ? ? ? ? ? ? ? ? ? numBytes =ins.read(mmBuffer);
? ? ? ? ? ? ? ? ? ? if (numBytes >0) {
@SuppressLint({"NewApi", "LocalSuppress"}) String order =new String(mmBuffer, 0, mmBuffer.length, "GBK").trim();
? ? ? ? ? ? ? ? ? ? ? ? if (null !=this.listener && !TextUtils.isEmpty(order)) {
this.listener.onBlueToothUtilsListener(order, this.bluetoothSocket);
? ? ? ? ? ? ? ? ? ? ? ? }
}
}catch (IOException e) {
e.printStackTrace();
? ? ? ? ? ? ? ? }finally {
this.mmBuffer =null;
? ? ? ? ? ? ? ? ? ? if (numBytes <=0) {
stopListener();
break;
? ? ? ? ? ? ? ? ? ? }
}
}
});
? ? ? ? receiveThread.start();
? ? }
public void stopListener() {
this.isRunning =false;
? ? ? ? if (null !=receiveThread) {
receiveThread.interrupt();
? ? ? ? ? ? receiveThread =null;
? ? ? ? }
}
@SuppressLint("NewApi")
public void sendMessage(String message) {
if (TextUtils.isEmpty(message))return;
? ? ? ? MyThreadPool.getInstance().newSingleThread(() -> {
if (null !=ops) {
try {
ops.write(message.trim().getBytes("GBK"));
? ? ? ? ? ? ? ? ? ? ops.flush();
? ? ? ? ? ? ? ? }catch (IOException e) {
e.printStackTrace();
? ? ? ? ? ? ? ? }
}
});
? ? }
}
3.MainActivity界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:app="http://schemas.android.com/apk/res-auto"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical"
? ? tools:context=".MainActivity">
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content">
? ? ? ? ? ? android:id="@+id/startBlueTooth"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="開啟藍(lán)牙"/>
? ? ? ? ? ? android:id="@+id/stopBlueTooth"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="關(guān)閉藍(lán)牙"
? ? ? ? ? ? android:layout_centerInParent="true"/>
? ? ? ? ? ? android:id="@+id/scanBlueTooth"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="掃描設(shè)備"
? ? ? ? ? ? android:layout_alignParentRight="true"/>
? ? ? ? android:id="@+id/et"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:inputType="text"
? ? ? ? android:hint="請(qǐng)輸入命令"
? ? ? ? android:layout_marginTop="5dp"
? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? android:layout_marginRight="40dp"/>
? ? ? ? android:id="@+id/sendMessage"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_marginTop="5dp"
? ? ? ? android:text="發(fā)送信息"/>
? ? ? ? android:id="@+id/recyclerView"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:layout_marginTop="2dp"
? ? ? ? android:orientation="vertical"
? ? ? ? android:background="#eeeeee"
? ? ? ? app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
4.MainActivity調(diào)用
public class MainActivityextends AppCompatActivityimplements View.OnClickListener
, ScanBroadcastReceiver.ScanBroadcastResultListener
, BlueToothStateBroadcastReceiver.BlueToothStateBroadcastReceiverListener
, BlueDeviceAdapter.BlueDeviceListener
, BlueToothUtils.BlueToothUtilsListener {
private static final int REQUEST_ENABLE_BT =1005;
? ? private static final int SACN_TIME =10 *1000;
? ? private ButtonstartBlueTooth, stopBlueTooth, scanBlueTooth;
? ? private RecyclerViewrecyclerView;
? ? private ListbluetoothDevices;
? ? private ListnoPairedBluetoothDevices;
? ? private ListpairedBluetoothDevices;
? ? private BlueDeviceAdaptermAdapter;
? ? private ScanBroadcastReceiverscanBroadcastReceiver;
? ? private BlueToothStateBroadcastReceiverblueToothStateBroadcastReceiver;
? ? private BlueToothUtilsblueToothUtils;
? ? private EditTextet;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? blueToothUtils = BlueToothUtils.getInstance(this);
? ? ? ? blueToothUtils.start();
? ? ? ? initView();
? ? ? ? initRecy();
? ? ? ? setAdapter();
? ? ? ? setListeners();
? ? ? ? register();
? ? ? ? getPairedList();
? ? ? ? ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION
? ? ? ? ? ? ? ? ? ? ? ? , Manifest.permission.BLUETOOTH
? ? ? ? ? ? ? ? ? ? ? ? , Manifest.permission.BLUETOOTH_ADMIN},
? ? ? ? ? ? ? ? 19);
? ? ? ? if (isBlueBoothEnabled()) {
exposeBlueTooth();
? ? ? ? }
}
private void getUnPairList() {
if (isBlueBoothAvailable()) {
if (BluetoothAdapter.getDefaultAdapter().isDiscovering()) {
Toast.makeText(this, "掃描中...", Toast.LENGTH_SHORT).show();
return;
? ? ? ? ? ? }
boolean isOk = BluetoothAdapter.getDefaultAdapter().startDiscovery();
? ? ? ? ? ? Handler handler =new Handler(new Handler.Callback() {
@Override
? ? ? ? ? ? ? ? public boolean handleMessage(@NonNull Message message) {
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
return true;
? ? ? ? ? ? ? ? }
});
? ? ? ? ? ? if (isOk) {
handler.sendEmptyMessageDelayed(0, SACN_TIME);
? ? ? ? ? ? }
}
}
private void getPairedList() {
List datas = getBlueDeviceList();
? ? ? ? for (BluetoothDevice device : datas) {
BlueDevice blueDevice =new BlueDevice();
? ? ? ? ? ? blueDevice.bluetoothDevice = device;
? ? ? ? ? ? blueDevice.isPaired =true;
? ? ? ? ? ? pairedBluetoothDevices.add(blueDevice);
? ? ? ? }
bluetoothDevices.addAll(pairedBluetoothDevices);
? ? ? ? mAdapter.notifyDataSetChanged();
? ? }
private void initRecy() {
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
? ? ? ? ? ? public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
? ? ? ? ? ? ? ? int index = parent.getChildAdapterPosition(view);
? ? ? ? ? ? ? ? if (index ==0) {
outRect.top = DensityUtil.dp2px(MainActivity.this, 2);
? ? ? ? ? ? ? ? }
outRect.bottom = DensityUtil.dp2px(MainActivity.this, 2);
? ? ? ? ? ? }
});
? ? }
private void setAdapter() {
noPairedBluetoothDevices =new ArrayList<>();
? ? ? ? pairedBluetoothDevices =new ArrayList<>();
? ? ? ? bluetoothDevices =new ArrayList<>();
? ? ? ? mAdapter =new BlueDeviceAdapter(bluetoothDevices);
? ? ? ? mAdapter.setOnBlueDeviceListener(this);
? ? ? ? recyclerView.setAdapter(mAdapter);
? ? }
private void register() {
registerBlueStateReceiver();
? ? ? ? registerBlueScanReceiver();
? ? }
private void registerBlueScanReceiver() {
scanBroadcastReceiver =new ScanBroadcastReceiver();
? ? ? ? scanBroadcastReceiver.setOnScanBroadcastResultListener(this);
? ? ? ? IntentFilter filter =new IntentFilter(BluetoothDevice.ACTION_FOUND);
? ? ? ? registerReceiver(scanBroadcastReceiver, filter);
? ? }
private void registerBlueStateReceiver() {
blueToothStateBroadcastReceiver =new BlueToothStateBroadcastReceiver();
? ? ? ? blueToothStateBroadcastReceiver.setOnBlueToothStateBroadcastReceiverListener(this);
? ? ? ? IntentFilter intentFilter =new IntentFilter();
? ? ? ? // 監(jiān)視藍(lán)牙關(guān)閉和打開的狀態(tài)
? ? ? ? intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
? ? ? ? // 監(jiān)視藍(lán)牙設(shè)備與APP連接的狀態(tài)
? ? ? ? intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
? ? ? ? intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
? ? ? ? intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
? ? ? ? //配對(duì)的狀態(tài)
? ? ? ? intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
? ? ? ? // 注冊(cè)廣播
? ? ? ? registerReceiver(blueToothStateBroadcastReceiver, intentFilter);
? ? }
private void setListeners() {
startBlueTooth.setOnClickListener(this);
? ? ? ? stopBlueTooth.setOnClickListener(this);
? ? ? ? scanBlueTooth.setOnClickListener(this);
? ? ? ? findViewById(R.id.sendMessage).setOnClickListener(view -> {
String message =et.getText().toString().trim();
? ? ? ? ? ? if (TextUtils.isEmpty(message))return;
? ? ? ? ? ? blueToothUtils.sendMessage(message);
? ? ? ? });
? ? ? ? blueToothUtils.setOnBlueToothUtilsListener(this);
? ? }
private void initView() {
startBlueTooth = findViewById(R.id.startBlueTooth);
? ? ? ? stopBlueTooth = findViewById(R.id.stopBlueTooth);
? ? ? ? recyclerView = findViewById(R.id.recyclerView);
? ? ? ? scanBlueTooth = findViewById(R.id.scanBlueTooth);
? ? ? ? et = findViewById(R.id.et);
? ? }
@Override
? ? public void onClick(View view) {
if (!isBlueBoothAvailable()) {
Toast.makeText(this, "藍(lán)牙設(shè)備不可用", Toast.LENGTH_SHORT).show();
return;
? ? ? ? }
boolean isEnabled = isBlueBoothEnabled();
? ? ? ? switch (view.getId()) {
case R.id.startBlueTooth:
if (isEnabled) {
Toast.makeText(this, "藍(lán)牙已開啟", Toast.LENGTH_SHORT).show();
return;
? ? ? ? ? ? ? ? }
Intent enableBtIntent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
? ? ? ? ? ? ? ? startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
break;
? ? ? ? ? ? case R.id.stopBlueTooth:
if (!isEnabled) {
Toast.makeText(this, "藍(lán)牙已關(guān)閉", Toast.LENGTH_SHORT).show();
return;
? ? ? ? ? ? ? ? }
BluetoothAdapter.getDefaultAdapter().disable();
break;
? ? ? ? ? ? case R.id.scanBlueTooth:
getUnPairList();
break;
? ? ? ? }
}
@Override
? ? public void onBlueToothStateBroadcastReceiver(int blueToothSate, BluetoothDevice device) {
if (blueToothSate == BluetoothAdapter.STATE_ON) {
exposeBlueTooth();
? ? ? ? ? ? List datas = getBlueDeviceList();
? ? ? ? ? ? for (BluetoothDevice btc : datas) {
BlueDevice blueDevice =new BlueDevice();
? ? ? ? ? ? ? ? blueDevice.bluetoothDevice = btc;
? ? ? ? ? ? ? ? blueDevice.isPaired =true;
? ? ? ? ? ? ? ? pairedBluetoothDevices.add(blueDevice);
? ? ? ? ? ? }
bluetoothDevices.addAll(0, pairedBluetoothDevices);
? ? ? ? ? ? mAdapter.notifyItemRangeInserted(0, pairedBluetoothDevices.size());
? ? ? ? }
if (blueToothSate == BluetoothAdapter.STATE_OFF) {
bluetoothDevices.clear();
? ? ? ? ? ? pairedBluetoothDevices.clear();
? ? ? ? ? ? noPairedBluetoothDevices.clear();
? ? ? ? ? ? mAdapter.notifyDataSetChanged();
? ? ? ? }
}
@Override
? ? public void onBlueToothPairedStateBroadcastReceiver(int pairState, BluetoothDevice device) {
if (null == device)return;
? ? ? ? BlueDevice blueDevice = getBlueDeviceFromList(device);
? ? ? ? if (pairState == BluetoothDevice.BOND_BONDED) {
if (null == blueDevice) {
blueDevice =new BlueDevice();
? ? ? ? ? ? ? ? blueDevice.isPaired =true;
? ? ? ? ? ? ? ? blueDevice.bluetoothDevice = device;
? ? ? ? ? ? }else {
noPairedBluetoothDevices.remove(blueDevice);
? ? ? ? ? ? ? ? int index =bluetoothDevices.indexOf(blueDevice);
? ? ? ? ? ? ? ? bluetoothDevices.remove(blueDevice);
? ? ? ? ? ? ? ? mAdapter.notifyItemRemoved(index);
? ? ? ? ? ? ? ? blueDevice.bluetoothDevice = device;
? ? ? ? ? ? ? ? blueDevice.isPaired =true;
? ? ? ? ? ? }
pairedBluetoothDevices.add(blueDevice);
? ? ? ? ? ? if (noPairedBluetoothDevices.size() ==0) {
bluetoothDevices.add(blueDevice);
? ? ? ? ? ? }else {
bluetoothDevices.add(pairedBluetoothDevices.size() -1, blueDevice);
? ? ? ? ? ? }
mAdapter.notifyItemInserted(bluetoothDevices.indexOf(blueDevice));
? ? ? ? }
if (pairState == BluetoothDevice.BOND_NONE) {
if (null == blueDevice) {
blueDevice =new BlueDevice();
? ? ? ? ? ? ? ? blueDevice.isPaired =false;
? ? ? ? ? ? ? ? blueDevice.bluetoothDevice = device;
? ? ? ? ? ? }else {
pairedBluetoothDevices.remove(blueDevice);
? ? ? ? ? ? ? ? int index =bluetoothDevices.indexOf(blueDevice);
? ? ? ? ? ? ? ? bluetoothDevices.remove(blueDevice);
? ? ? ? ? ? ? ? mAdapter.notifyItemRemoved(index);
? ? ? ? ? ? ? ? blueDevice.bluetoothDevice = device;
? ? ? ? ? ? ? ? blueDevice.isPaired =false;
? ? ? ? ? ? }
noPairedBluetoothDevices.add(blueDevice);
? ? ? ? ? ? bluetoothDevices.add(blueDevice);
? ? ? ? ? ? mAdapter.notifyItemInserted(bluetoothDevices.indexOf(blueDevice));
? ? ? ? }
}
@Override
? ? public void onBlueToothLinkedStateBroadcastReceiver(String action, BluetoothDevice device) {
if (null == device)return;
? ? ? ? BlueDevice blueDevice = getBlueDeviceFromList(device);
? ? ? ? if (null == blueDevice)return;
? ? ? ? if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
blueDevice.isConnected =true;
? ? ? ? }
if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
blueDevice.isConnected =false;
? ? ? ? }
mAdapter.notifyItemChanged(bluetoothDevices.indexOf(blueDevice));
? ? }
@Override
? ? public void onBlueDeviceListener(int position) {
BlueDevice bd =bluetoothDevices.get(position);
? ? ? ? if (null != bd && !bd.isConnected &&null != bd.bluetoothDevice) {
blueToothUtils.connect(bd.bluetoothDevice);
? ? ? ? }
}
private void exposeBlueTooth() {
Intent discoverableIntent =
new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
? ? ? ? discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
? ? ? ? discoverableIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? startActivity(discoverableIntent);
? ? }
@Override
? ? public void onScanBroadcastResultListener(BluetoothDevice bluetoothDevice) {
BlueDevice blueDevice = getBlueDeviceFromList(bluetoothDevice);
? ? ? ? if (null == blueDevice) {
blueDevice =new BlueDevice();
? ? ? ? ? ? blueDevice.bluetoothDevice = bluetoothDevice;
? ? ? ? ? ? noPairedBluetoothDevices.add(blueDevice);
? ? ? ? ? ? bluetoothDevices.add(blueDevice);
? ? ? ? ? ? mAdapter.notifyItemInserted(bluetoothDevices.size() -1);
? ? ? ? }else {
blueDevice.bluetoothDevice = bluetoothDevice;
? ? ? ? ? ? mAdapter.notifyItemChanged(bluetoothDevices.indexOf(blueDevice));
? ? ? ? }
}
public BlueDevicegetBlueDeviceFromNoPairList(BluetoothDevice bluetoothDevice) {
if (TextUtils.isEmpty(bluetoothDevice.getAddress()))return null;
? ? ? ? for (BlueDevice blueDevice :noPairedBluetoothDevices) {
if (blueDevice.bluetoothDevice.getAddress().equals(bluetoothDevice.getAddress()))
return blueDevice;
? ? ? ? }
return null;
? ? }
public BlueDevicegetBlueDeviceFromPairList(BluetoothDevice bluetoothDevice) {
if (TextUtils.isEmpty(bluetoothDevice.getAddress()))return null;
? ? ? ? for (BlueDevice blueDevice :pairedBluetoothDevices) {
if (blueDevice.bluetoothDevice.getAddress().equals(bluetoothDevice.getAddress()))
return blueDevice;
? ? ? ? }
return null;
? ? }
public BlueDevicegetBlueDeviceFromList(BluetoothDevice bluetoothDevice) {
if (TextUtils.isEmpty(bluetoothDevice.getAddress()))return null;
? ? ? ? for (BlueDevice blueDevice :bluetoothDevices) {
if (blueDevice.bluetoothDevice.getAddress().equals(bluetoothDevice.getAddress()))
return blueDevice;
? ? ? ? }
return null;
? ? }
private ListgetBlueDeviceList() {
if (!isBlueBoothAvailable())return new ArrayList<>();
? ? ? ? Set pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
? ? ? ? List devices =new ArrayList<>();
? ? ? ? if (pairedDevices.size() >0) {
devices.addAll(pairedDevices);
? ? ? ? }
return devices;
? ? }
private boolean isBlueBoothAvailable() {
return null != BluetoothAdapter.getDefaultAdapter();
? ? }
private boolean isBlueBoothEnabled() {
if (!isBlueBoothAvailable())return false;
? ? ? ? return BluetoothAdapter.getDefaultAdapter().isEnabled();
? ? }
@Override
? ? protected void onDestroy() {
super.onDestroy();
? ? ? ? scanBroadcastReceiver.setOnScanBroadcastResultListener(null);
? ? ? ? blueToothStateBroadcastReceiver.setOnBlueToothStateBroadcastReceiverListener(null);
? ? ? ? unregisterReceiver(scanBroadcastReceiver);
? ? ? ? unregisterReceiver(blueToothStateBroadcastReceiver);
? ? ? ? scanBroadcastReceiver =null;
? ? ? ? blueToothStateBroadcastReceiver =null;
? ? ? ? blueToothUtils.onDestroy();
? ? }
@Override
? ? public void onBlueToothUtilsListener(String message, BluetoothSocket bluetoothSocket) {
runOnUiThread(() -> {
Log.e("阿斯達(dá)所多啊所","value = "+message);
? ? ? ? ? ? Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
? ? ? ? });
? ? }
}