首先一定要設權限  不然程式沒錯誤 一樣會一直FC!!!

BLUETOOTH             讓程式有權限連接裝置、傳輸資料。
BLUETOOTH_ADMIN 讓程式有權限搜尋裝置及設定藍芽。


 

下面提供一些BluetoothAdapter 基本的方法

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();//獲得當前的藍芽

if(adapter!=null){
   此手機有支援藍芽
}else{
   此手機無支援藍芽
}

 

if(adapter.isEnabled()!=true){//如果藍芽未開啟
     //則打開藍芽(會問使用者)
     Intent enabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
     startActivity(enabler);

     //則打開藍芽(不問使用者)
      adapter.enable();

}else{
     藍芽已開啟
}

-----------------------------------------------

藍芽名稱:adapter.getName()
藍芽位址:adapter.getAddress()
藍芽adapter狀態:adapter.getState()

adapter.disable();//關閉藍芽

------------------------------------------------

//開放讓別人搜尋自己設備的藍芽300秒

Intent di = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
di.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(di);


arrow
arrow

    cookiesp 發表在 痞客邦 留言(0) 人氣()