Android蓝牙发现和显示设备

时间:2013-09-11 10:28:02

标签: java android bluetooth

我想创建一个简单的应用程序,我想在其中显示蓝牙设备..但是当我按下按钮时,应用程序崩溃.. 在ActivityMain.java中,这个方法将转到下一个活动(Activity.java)..其中我遇到问题。按钮的恢复正在工作

ActivityMain.java

package com.example.bluetoothcheck5;


import java.util.Set;

import com.example.bluetoothcheck5.Activity2;
import android.os.Bundle;
import android.app.Activity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
  private static final int REQUEST_ENABLE_BT = 0;
    private static final int REQUEST_DISCOVERABLE_BT = 0;
  @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   final TextView out=(TextView)findViewById(R.id.out);
   final Button button = (Button) findViewById(R.id.button1);
  final Button button1 = (Button) findViewById(R.id.button2);
  final Button button2 = (Button) findViewById(R.id.button3);
  final Button buttonSearch = (Button) findViewById(R.id.buttonSearch);
  final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
   if (mBluetoothAdapter == null) {
    out.append("device not supported");
  }
  button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }
  });
  button1.setOnClickListener(new View.OnClickListener() {
 @Override
    public void onClick(View arg0) {
        if (!mBluetoothAdapter.isDiscovering()) {               
               Context context = getApplicationContext();
               CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE";
               int duration = Toast.LENGTH_SHORT;

               Toast toast = Toast.makeText(context, text, duration);
               toast.show();
            Intent enableBtIntent = new         Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);

        }
    }
});
button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {   
        mBluetoothAdapter.disable();

        Context context = getApplicationContext();
           CharSequence text = "TURNING_OFF BLUETOOTH";

           Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
           toast.show();


           Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();
           if (mPairedDevices.size() > 0) 
           {
               for (BluetoothDevice mDevice : mPairedDevices) 
               {
                   Log.v("Title", "PairedDevices: " + mDevice.getName() + " " +         mDevice.getAddress());
               }
           }
        }
});
buttonSearch.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {


        // TODO Auto-generated method stub
        Intent myIntent = new Intent(arg0.getContext(), Activity2.class);
    //   myIntent.setClass(MainActivity.this, Activity2.class);
          // startActivityForResult(myIntent, REQUEST_PAIRED_DEVICE); 

        startActivity(myIntent);



    }
});


}}

Activity2.java

package com.example.bluetoothcheck5;


import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;



import com.example.bluetoothcheck5.R;
import com.example.bluetoothcheck5.MainActivity;


public class Activity2 extends MainActivity {
   protected static final String TAG = "TAG";
   private BluetoothAdapter mBluetoothAdapter;
   private ArrayAdapter<String> mPairedDevicesArrayAdapter;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,   R.layout.activity_2);

        ListView mPairedListView = (ListView) findViewById(R.id.listView1);
        mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
        mPairedListView.setOnItemClickListener(mDeviceClickListener);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();

        if (mPairedDevices.size() > 0) 
        {
            findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
            for (BluetoothDevice mDevice : mPairedDevices) 
            {
                mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" +   mDevice.getAddress());
            }
        } 
        else 
        {
            String mNoDevices =    getResources().getText(R.string.none_paired).toString();
            mPairedDevicesArrayAdapter.add(mNoDevices);
        }
    }

 private OnItemClickListener mDeviceClickListener = new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> mAdapterView, View mView, int   mPosition, long mLong) 
        {
            mBluetoothAdapter.cancelDiscovery();
            String mDeviceInfo = ((TextView) mView).getText().toString();
            String mDeviceAddress = mDeviceInfo.substring(mDeviceInfo.length() -    17);
            Log.v(TAG, "Device_Address " + mDeviceAddress);

            Bundle mBundle = new Bundle();
            mBundle.putString("DeviceAddress", mDeviceAddress);
            Intent mBackIntent = new Intent();
            mBackIntent.putExtras(mBundle);
            setResult(Activity.RESULT_OK, mBackIntent);
            finish();
        }
    };


 }

1 个答案:

答案 0 :(得分:0)

我做了示例项目来演示蓝牙消失,配对或非配对设备。在这里找到它,

http://www.londatiga.net/it/programming/android/how-to-programmatically-scan-or-discover-android-bluetooth-device/