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();
}
}
}
蓝牙帮助类
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一般来说,喜欢站立的人,比喜欢躺着或或坐着的人,身材更好。与躺着的姿势相比,站姿所消耗的能量要多出10%。而单腿站...
- 《稀缺》这本书,我在几个月以前看过,书中说了很多的现象都是大部分人生活中常有的,比如工作又忙又累又穷,成天加...