package com.sie.isky.android.helper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.util.Log;
import com.sie.isky.android.bean.PrinterBean;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
* 时 间: 2017/8/7
* 作 者: 郑亮
* Q Q : 1023007219
*/
public class BlueToothHelper {
private BluetoothAdapter bluetoothAdapter = null;
public BlueToothHelper() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
public BluetoothAdapter getAdapter() {
return bluetoothAdapter;
}
public BluetoothDevice getDeviceByMac(String mac) {
return bluetoothAdapter.getRemoteDevice(mac);
}
/**
* 是否支持蓝牙
*/
public boolean isSupportBlueTooth() {
if (bluetoothAdapter != null) {
return true;
} else {
return false;
}
}
/**
* 蓝牙是否打开
*
* @return
*/
@SuppressLint("Assert")
public boolean getSwitchBlueTooth() {
assert (bluetoothAdapter != null);
return bluetoothAdapter.isEnabled();
}
/**
* 打开蓝牙
*
* @param activity
*/
public void trueOnBlueTooth(Activity activity) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivity(intent);
}
/**
* 关闭蓝牙
*/
public void trueOffBlueTooth() {
bluetoothAdapter.disable();
}
/**
* 查找设备
*/
@SuppressLint("Assert")
public void findDevice() {
assert (bluetoothAdapter != null);
bluetoothAdapter.startDiscovery();
}
/**
* 取消查找设备
*/
@SuppressLint("Assert")
public void cancelDevice() {
assert (bluetoothAdapter != null);
bluetoothAdapter.cancelDiscovery();
}
/**
* 获取绑定设备
*
* @return
*/
public List<PrinterBean> getBlueToothDeviceList() {
List<PrinterBean> printerBeans = new ArrayList<PrinterBean>();
Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice bondedDevice : bondedDevices) {
PrinterBean printerBean = new PrinterBean();
printerBean.setMac(bondedDevice.getAddress());
printerBean.setName(bondedDevice.getName());
printerBean.setUuid(Arrays.toString(bondedDevice.getUuids()));
printerBeans.add(printerBean);
}
return printerBeans;
}
// 与设备解除配对
public boolean removeBond(Class btClass, BluetoothDevice btDevice) throws Exception {
Method removeBondMethod = btClass.getMethod("removeBond");
Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
return returnValue.booleanValue();
}
// 与设备配对
public boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception {
Method createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
return returnValue.booleanValue();
}
/**
*
* @param clsShow
*/
public void printAllInform(Class clsShow) {
try {
// 取得所有方法
Method[] hideMethod = clsShow.getMethods();
int i = 0;
for (; i < hideMethod.length; i++) {
Log.e("method name", hideMethod[i].getName());
}
// 取得所有常量
Field[] allFields = clsShow.getFields();
for (i = 0; i < allFields.length; i++) {
Log.e("Field name", allFields[i].getName());
}
} catch (SecurityException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (IllegalArgumentException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
蓝牙帮助类
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 一般来说,喜欢站立的人,比喜欢躺着或或坐着的人,身材更好。与躺着的姿势相比,站姿所消耗的能量要多出10%。而单腿站...
- 《稀缺》这本书,我在几个月以前看过,书中说了很多的现象都是大部分人生活中常有的,比如工作又忙又累又穷,成天加...