如何使用藍牙
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//獲取藍牙適配器
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
@OnClick({R.id.btn_start, R.id.btn_stop, R.id.btn_search, R.id.btn_discoverable})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_start:
bluetoothAdapter.enable();
Toast.makeText(MainActivity.this,"藍牙已打開",Toast.LENGTH_SHORT).show();
break;
case R.id.btn_stop:
bluetoothAdapter.disable();
Toast.makeText(MainActivity.this,"藍牙已關閉",Toast.LENGTH_SHORT).show();
break;
case R.id.btn_discoverable:
discoverableBluetooth();
break;
case R.id.btn_search:
searchBluetooh();
break;
}
}
/**
* 打開藍牙的可檢測性
*/
private void discoverableBluetooth() {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(intent,0);
Toast.makeText(MainActivity.this,"藍牙可檢測性已打開",Toast.LENGTH_SHORT).show();
}
//搜索藍牙
private void searchBluetooh() {
//添加藍牙可以被發現
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
MBluetoothBroadCast mBluetoothBroadCast = new MBluetoothBroadCast();
registerReceiver(mBluetoothBroadCast,intentFilter);
IntentFilter intentFilter1 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mBluetoothBroadCast,intentFilter1);
//開始找藍牙設備
bluetoothAdapter.startDiscovery();
}
//符合我註冊的過濾器 ,就可以進入onReceive方法
class MBluetoothBroadCast extends BroadcastReceiver{
//一旦被發現或鏈接,我們就能能收到廣播;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//如果我們搜索到了其他的藍牙,就會進入這個判斷
if( BluetoothDevice.ACTION_FOUND.equals(action)){
//得到搜索到的藍牙設備
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(device.getBondState()!=BluetoothDevice.BOND_BONDED){
//用設備獲取藍牙名字
System.out.println(device.getName()+",,,,,,,,,,,,,,,,");
}
//如果搜索完畢,會進入這個判斷;
}else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
Toast.makeText(MainActivity.this, "已搜數完成", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//獲取藍牙適配器
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
@OnClick({R.id.btn_start, R.id.btn_stop, R.id.btn_search, R.id.btn_discoverable})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_start:
bluetoothAdapter.enable();
Toast.makeText(MainActivity.this,"藍牙已打開",Toast.LENGTH_SHORT).show();
break;
case R.id.btn_stop:
bluetoothAdapter.disable();
Toast.makeText(MainActivity.this,"藍牙已關閉",Toast.LENGTH_SHORT).show();
break;
case R.id.btn_discoverable:
discoverableBluetooth();
break;
case R.id.btn_search:
searchBluetooh();
break;
}
}
/**
* 打開藍牙的可檢測性
*/
private void discoverableBluetooth() {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(intent,0);
Toast.makeText(MainActivity.this,"藍牙可檢測性已打開",Toast.LENGTH_SHORT).show();
}
//搜索藍牙
private void searchBluetooh() {
//添加藍牙可以被發現
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
MBluetoothBroadCast mBluetoothBroadCast = new MBluetoothBroadCast();
registerReceiver(mBluetoothBroadCast,intentFilter);
IntentFilter intentFilter1 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mBluetoothBroadCast,intentFilter1);
//開始找藍牙設備
bluetoothAdapter.startDiscovery();
}
//符合我註冊的過濾器 ,就可以進入onReceive方法
class MBluetoothBroadCast extends BroadcastReceiver{
//一旦被發現或鏈接,我們就能能收到廣播;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//如果我們搜索到了其他的藍牙,就會進入這個判斷
if( BluetoothDevice.ACTION_FOUND.equals(action)){
//得到搜索到的藍牙設備
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(device.getBondState()!=BluetoothDevice.BOND_BONDED){
//用設備獲取藍牙名字
System.out.println(device.getName()+",,,,,,,,,,,,,,,,");
}
//如果搜索完畢,會進入這個判斷;
}else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
Toast.makeText(MainActivity.this, "已搜數完成", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.