android 蓝牙操作的demo
demo:下载地址
打开权限:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
源码:
package com.bobo.study.study_3_1;
//在Mainfest中注册相关权限
import android.app.Activity;
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.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Iterator;
import java.util.Set;
public class MainActivity extends Activity implements View.OnClickListener{
Button but1,but2,but3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1 = (Button) findViewById(R.id.but1);
but1.setOnClickListener(this);
but2 = (Button) findViewById(R.id.but2);
but2.setOnClickListener(this);
but3 = (Button) findViewById(R.id.but3);
but3.setOnClickListener(this);
IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
BluetoothReceiver receiver=new BluetoothReceiver();
registerReceiver(receiver, filter);
}
private class BluetoothReceiver extends BroadcastReceiver {
@Override
//接受系统的广播
public void onReceive(Context context, Intent intent) {
//过滤出系统的关于找到的蓝牙设备的广播
BluetoothDevice blueDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(MainActivity.this,blueDevice.getAddress(),Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
//BluetoothAdapter代表本地的蓝牙适配器
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (v == but1) {
//设置本机蓝牙在500S内可被其它蓝牙检测到
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
//调用系统界面提示用户选择
startActivity(intent);
} else if (v == but2) {
//扫描周围的蓝牙设备
adapter.startDiscovery();//每扫描到一个蓝牙设备,系统会自动发送一个广播
}else if(v==but3){
if (adapter != null) {//判断手机是否有蓝牙设备
if (!adapter.isEnabled()) {//判断当前蓝牙设备是否可用
//跳出一个界面,提示用户是否打开蓝牙(该界面由系统提供)
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
//BluetoothDevice代表远程的蓝牙适配器(即你要连接的目标的蓝牙)
Set<BluetoothDevice> devices = adapter.getBondedDevices();
if (devices.size() > 0) {
for (Iterator iterator = devices.iterator(); iterator.hasNext();) {
BluetoothDevice device = (BluetoothDevice) iterator.next();
//显示远程蓝牙设备的地址
Toast.makeText(this, device.getAddress(), Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(this, "本机没有蓝牙设备", Toast.LENGTH_LONG).show();
}
}
}
}