0

I want to create simple application in which i want to show bluetooth devices..but when i press the button, Application crashes.. in ActivityMain.java this method will go to next activity (Activity.java)..in which i am getting problem..Rest of buttons are working

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();
        }
    };


 }
4

1 回答 1

0

我制作了示例项目来演示蓝牙发现、配对或取消配对设备。在这里找到它,

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

于 2014-02-20T16:03:33.667 回答