package com.moka;
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.content.ContextWrapper;
/** 类名:LBPS(Local BlueTooth Position Service)
*功能:取定位信息,以系统广播的方式将定位信息通知给应用层
*要求:只能在Android2.0及以上版本使用;使用此服务需要添加BLUETOOTH_ADMIN和BLUETOOTH权限
*@author ltu
*@version v1.0.0
*/
public class LBPS extends ContextWrapper{
/**LBPS 广播定位信息的广播动作*/
public static final String ACTION_LBPS_FINISHED = "com.moka.lbps.LBPS_RESULT";
/**LBPS 提取定位信息 */
public static final String EXTRA_LBPS_POSITION = "com.moka.lbps.position";
/**LBPS 提取定位状态*/
public static final String EXTRA_LBPS_STATE = "com.moka.lbps.result";
/**LBPS 提取失败描述*/
public static final String EXTRA_LBPS_FAILE_MESSAGE = "com.moka.lbps.failemessage";
public static final int ERROR = 0x00;
public static final int STATE_LBPS_OK = 0x01;
public static final int STATE_LBPS_FAILE = 0x02;
/**蓝牙广播消息接收器 */
private BTBReceiver btbReceiver = null;
/**蓝牙定位装置MAC地址*/
private String btMac;
/**定位信息,默认值为"null"*/
private String lbpsPosition = "null";
/**记录是否定位成功*/
private boolean isLbpsOk = false;
/**构造函数,LBPS(Context context,String Mac)
*@param Context context:实例化LBPS的Context引用
*@param String Mac :蓝牙定位装置中固定的蓝牙适配器地址*/
public LBPS(Context context, String mac){
super(context);
btMac = mac;
init();
}
/**初始化LBPS服务*/
private void init(){
btbReceiver = new BTBReceiver();
if(btbReceiver != null){
//注册本地蓝牙适配器状态改变接收器
registerReceiver(btbReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
//注册用于接收已经搜索到的蓝牙设备
registerReceiver(btbReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
//注册本地蓝牙适配器状态改变接收器
registerReceiver(btbReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
}
}
/**停止LBPS服务*/
public void StopService(){
if(null != btbReceiver){
unregisterReceiver(btbReceiver);
}
}
/**启动LBPS服务*/
public void StartService(){
if (!BTAdmin.getInstance().IsEnable()){
broadcast(false, "蓝牙原本关闭,请求打开蓝牙");
BTAdmin.getInstance().Open();
}else{
broadcast(false, "scan...");
beginSearch();
}
}
/**开始搜索定位信息*/
private void beginSearch(){
//如果正在搜索,先取消搜索
if(BTAdmin.getInstance().GetAdapter().isDiscovering()){
BTAdmin.getInstance().GetAdapter().cancelDiscovery();
}
BTAdmin.getInstance().GetAdapter().startDiscovery();
}
/**广播LBPS定位结果*/
private void broadcast(boolean isFond, String position){
Intent broadcastIndtent = new Intent(ACTION_LBPS_FINISHED);
//设置定位信息广播数据
String name = (isFond)?EXTRA_LBPS_POSITION:EXTRA_LBPS_FAILE_MESSAGE;
broadcastIndtent.putExtra(name, position);
//设置定位状态广播数据
int lbpsResult = (isFond)?STATE_LBPS_OK:STATE_LBPS_FAILE;
broadcastIndtent.putExtra(EXTRA_LBPS_STATE, lbpsResult);
sendBroadcast(broadcastIndtent);
}
//定义广播事件消息接收器
private class BTBReceiver extends BroadcastReceiver{
public void onReceive(Context c, Intent intent) {
String action = intent.getAction();
//蓝牙适配器状态改变广播消息
if (action.endsWith(BluetoothAdapter.ACTION_STATE_CHANGED)){
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
String strState;
switch(state){
case BluetoothAdapter.STATE_OFF:
//不必要再打开蓝牙,因为在StartService方法中,如果蓝牙关闭,则会打开。
//如果此时打开蓝牙,则将无法关闭蓝牙适配器
//BTAdmin.getInstance().Open();
strState = "STATE_OFF";
break;
case BluetoothAdapter.STATE_ON:
strState = "STATE_ON";
beginSearch();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
strState="STATE_TURNING_OFF";
break;
case BluetoothAdapter.STATE_TURNING_ON:
strState = "STATE_TURNING_ON";
break;
default:
strState = "unkonw";
}
broadcast(false, strState);
}
//获得已经搜索到的蓝牙设备
else if(action.endsWith(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//如果是蓝牙定位装置,获取定位信息,取消搜索,发送定位信息广播
if(device.getAddress().endsWith(btMac)){
lbpsPosition = device.getName();
isLbpsOk = true;
BTAdmin.getInstance().GetAdapter().cancelDiscovery();
broadcast(true, lbpsPosition);
}
}
//搜索完毕
else if(action.endsWith(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
if (!isLbpsOk){
broadcast(false, lbpsPosition);
}
isLbpsOk = false;
}
}
}//结束BTBReceiver类定义
}
评论3