android蓝牙通信
package com.example.bluetoothpro;
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.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
BluetoothAdapter adapter;
Button open;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
open =(Button) findViewById(R.id.open);
MyListener listener = new MyListener();
open.setOnClickListener(listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class MyListener implements OnClickListener {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(open==arg0) {
adapter= BluetoothAdapter.getDefaultAdapter();
if(adapter==null) {
Toast.makeText(MainActivity.this,"not supoort the device",Toast.LENGTH_SHORT).show();
}else {
if(adapter.isEnabled()){
//已打开蓝牙
BluetoothRev receiver = new BluetoothRev();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
IntentFilter notfilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, notfilter);
adapter.startDiscovery();
unregisterReceiver(receiver);
}else {
//打开蓝牙
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
BluetoothRev receiver = new BluetoothRev();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
adapter.startDiscovery();
unregisterReceiver(receiver);
}
}
}
}
private class BluetoothRev extends BroadcastReceiver{
boolean isFound = false;
@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);
isFound = true;
Toast.makeText(MainActivity.this,device.getName()+","+device.getAddress(), Toast.LENGTH_SHORT).show();
}else
if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
if(isFound==false){
Toast.makeText(MainActivity.this,"not found",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"found",Toast.LENGTH_SHORT).show();
}
adapter.cancelDiscovery();
//MainActivity.this.unregisterReceiver(this);
}
}
}
}
}
更多相关文章
- android 打开 url 方式
- Android调用系统程序
- Android(安卓)自定义APP开机启动,并打开其它App
- Android遇到 android studio "Internal HTTP server disabled: C
- Android之蓝牙设备使用
- ADT下载地址整理
- Android(安卓)打开浏览器的几种方法
- 最新版 Android(安卓)SDK 无法获取SDK包 的解决方案
- Android闪光灯的打开与关闭