//开启蓝牙功能
myButton = (Button)findViewById(R.id.Button_1);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//得到BluetoothAdapter对象
BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
//判断BluetoothAdapter对象是否为空,为空,则表明本机没有蓝牙设备
if(null != adapter){
System.out.println("本机拥有蓝牙设备");
//判断蓝牙是否可用
if(!adapter.isEnabled()){
//创建一个intent对象,感对象用于启动一个Activity,提示用户开启蓝牙设备
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
//迭代,得到所有已经配对的蓝牙适配器对象, 本机已保存的配对信息。
Set devices = adapter.getBondedDevices();
if(devices.size() > 0){
for(Iterator iterator = devices.iterator(); iterator.hasNext(); ){
BluetoothDevice bluetoothDevice = (BluetoothDevice)iterator.next();
System.out.println(bluetoothDevice.getAddress()+ "----" +bluetoothDevice.getName());
}
}
}else{
System.out.println("本机没有蓝牙设备");
}
}
});
//设置蓝牙可见
myButton2 = (Button)findViewById(R.id.Button_2);
myButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//创建Intent对象,并将其Action的值设置为
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//讲一个键值对对方到Intent对象当中,用于指定可见状态的持续时间
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
startActivity(discoverableIntent);
}
});
myButton3 = (Button)findViewById(R.id.Button_3);
myButton3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
str = "";
//广播监听
IntentFilter intentfilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
bluetoothReceiver = new BluetoothReceiver();
registerReceiver(bluetoothReceiver, intentfilter);
BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
adapter.startDiscovery();
}
});
myButton4 = (Button)this.findViewById(R.id.Button_4);
myButton4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String address = "00:10:10:22:02:72"; //电脑MAC
UUID uuid = UUID.fromString(SPP_UUID);
//创建adapter对象
BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
//通过MAC地址创建BluetoothDevice对象
BluetoothDevice btDev = adapter.getRemoteDevice(address);
try {
btSocket = btDev.createRfcommSocketToServiceRecord(uuid);
btSocket.connect();
System.out.println("----"+btSocket.getRemoteDevice().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("创建RFCOM出错");
}
InputStream is =null;
OutputStream os = null;
try {
is = btSocket.getInputStream();
os = btSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
System.out.println("·······");
}
String message = "01123456";
byte[] send = message.getBytes();
try{
os.write(send);
} catch(IOException e) {
e.printStackTrace();
System.out.println("输出时出现异常");
}
}
});
}
//广播监听类
private class BluetoothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getName()+"----"+device.getAddress());
str += device.getName()+"----"+device.getAddress() + "\n";
myTextView.setText(str);
}
}
}
}
/*
* 未开启蓝牙功能的时候仍然能够得到已经配对的蓝牙的信息
* 已配对的蓝牙设备的信息已经存储在了手机里,但是不能进行连接操作
*
*/