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)