最近在做藍芽簽到功能時,了解了藍芽的一部分知識,雖然不夠深入,但是感覺良好,他的整體思路如下:
在指定時間內,開啟藍芽,掃瞄周邊藍芽訊號,如果有指定的藍芽,則由使用者確認連線藍芽,最終簽到成功.
首先,我們先寫乙個工具類,來判斷當前時間是否在指定時間之內:
/**
* 判斷時間是否在時間段內
**@param date
* 當前時間 yyyy-mm-dd hh:mm:ss
*@param strdatebegin
* 開始時間 00:00:00
*@param strdateend
* 結束時間 00:05:00
*@return
*/public
static
boolean
isindate(date date, string strdatebegin,
string strdateend) else
if (strdateh == strdatebeginh && strdatem >= strdatebeginm
&& strdatem <= strdateendm) else
if (strdateh == strdatebeginh && strdatem == strdatebeginm
&& strdates >= strdatebegins && strdates <= strdateends)
// 當前時間小時數大等於開始時間小時數,等於結束時間小時數,分鐘數小等於結束時間分鐘數
else
if (strdateh >= strdatebeginh && strdateh == strdateendh
&& strdatem <= strdateendm) else
if (strdateh >= strdatebeginh && strdateh == strdateendh
&& strdatem == strdateendm && strdates <= strdateends) else
} else
}
廣播註冊完成了,那麼接收呢?需要建立廣播接受者:mbluetoothadapter = bluetoothadapter.getdefaultadapter();
if(mbluetoothadapter ==null)
如果藍芽介面卡為空的話,說明自手機不支援藍芽,如果不為空的話,我們獲取他曾經連線過得藍芽裝置資訊.
else
}這裡的廣播是查詢藍芽,查詢完畢,和藍芽連線狀態發生改變時用於接受狀態.
//藍芽查詢,可以在reciever中接受查詢到的藍芽裝置
intentfilter mfilter = new intentfilter(bluetoothdevice.action_found);
registerreceiver(mreceiver, mfilter);
// 註冊搜尋完時的receiver
mfilter = new intentfilter(bluetoothadapter.action_discovery_finished);
registerreceiver(mreceiver, mfilter);
//藍芽連線狀態發生改變時,接收狀態
mfilter = new intentfilter(bluetoothdevice.action_bond_state_changed);
registerreceiver(mreceiver,mfilter);
}
private broadcastreceiver mreceiver = new broadcastreceiver() catch (exception e) }}
// 搜尋完成
} else
if (action
.equals(bluetoothadapter.action_discovery_finished)) else
if(action.equals(bluetoothdevice.action_bond_state_changed))}}
};
/**
* 藍芽配對
**@param btclass
*@param btdevice
*@return
*@throws exception
*/public
boolean
createbond(class btclass, bluetoothdevice btdevice) throws exception
這是使用者知曉的情況下,當使用者沒有手動去掃瞄藍芽,但是此時已經到了簽到時間,我們就會用到之前判斷當前時間是否在指定時間之內的方法,/**
* 判斷是否支援藍芽,並開啟藍芽
* 獲取到bluetoothadapter之後,還需要判斷是否支援藍芽,以及藍芽是否開啟。
* 如果沒開啟,需要讓使用者開啟藍芽:
*/public
void
checkbledevice(context context)
// 開始搜尋藍芽裝置,搜尋到的藍芽裝置通過廣播返回
mbluetoothadapter.startdiscovery();
}else
}
上面通過時間判斷開啟藍芽發出訊息,那麼,我們要做什麼呢,這要看你自己了,我是這麼幹的:,1.當時間到了之後,我接受到訊息,此時自動呼叫checkbledevice方法掃瞄藍芽,2.如果麼有到指定時間,我會依舊發出一條訊息,在對應訊息處理中,呼叫currenttime方法,判斷當前時間,在currenttime方法中,無論到沒到指定時間,都會發出一條訊息,會繼續在1和2中繼續判斷,迴圈往復,直到時間到了。/**
* 判斷是否到了指定時間,開啟藍芽,掃瞄指定裝置
*/public
void
currenttime()else
}
因為,藍芽連線成功後會記住,所以簽到成功後,我會移除指定的已配對的藍芽裝置,通過呼叫另乙個使用反射完成方法」:private handler handler = new handler()else
if(msg.what ==1) catch (exception e)
}else
if(msg.what == 2)else
if(msg.what == 3)
}};
最後由於我們註冊了廣播,所以,在頁面銷毀時,我們要解除註冊:/**
* 刪除指定的已配對裝置
*@param btclass
*@param btdevice
*@return
*@throws exception
*/public
boolean
removebond(class btclass,bluetoothdevice btdevice) throws exception
@override
protected
void
ondestroy()
android 藍芽重複掃瞄
我想在service內讓藍芽可以一直執行掃瞄,之前在activity內可以重複掃瞄,但移植到服務後就不能了請問要怎麼解決?public class search beacon extends service implements locationlistener override public vo...
Android 藍芽連線
一 概述 藍芽是一種無線技術標準,可實現固定裝置 移動裝置和樓宇個人域網之間的短距離資料交換。最多可以同時和7個其它藍芽裝置建立連線,進行通訊。藍芽可分為兩大類 傳統藍芽 藍芽3.0規範之前 低功耗藍芽 藍芽4.0規範之後 android 從4.3版本 api level 18 開始支援低功耗藍芽b...
Android 藍芽連線
今天要做乙個藍芽4.0的通訊,先做個小test,之後再看看具體的api override public void onclick view v 3,掃瞄附近的裝置 list devicelist new arraylist if bluetoothadapter.isdiscovering else...