我有一些蓝牙示例代码在mainactivity中运行良好。然后,我将其移至新的活动,并创建了一个按钮以运行页面。我可以进入活动并单击按钮来激活我的蓝牙功能,但是到线路时它会崩溃:

if (!myBluetoothAdapter.isEnabled())里面
Set_Up_Connection.java但是行
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(myBluetoothAdapter == null)工作正常。

主要活动


package com.evenfurtherbeyond.bluetoothsendrecieve;
//imports are created automatically when requested by functions

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Set;

public class MainActivity extends Activity {

    //define the settings button variable
    private Button settingsBtn;
    public BluetoothAdapter myBluetoothAdapter;
    protected Set<BluetoothDevice> pairedDevices;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // take an instance of BluetoothAdapter - Bluetooth radio
        myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        //on the apps creation set the content view to the activity_my.xml layout.
        setContentView(R.layout.activity_main);

        //define settings button
        settingsBtn = (Button) findViewById(R.id.settingsbt);

        //set up an on click listener
        settingsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //define an intent to start a new activity
                //for starting Activity because you will need to pass Application or component
                // context as first parameter to Intent Constructor when you are creating
                // intent for a specific component of application.
                Intent settingsIntent = new Intent(v.getContext(), Set_Up_Connection.class);
                //start the intent.
                startActivity(settingsIntent);
            }
        });
    }
}





建立连接



package com.evenfurtherbeyond.bluetoothsendrecieve;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Set;

public class Set_Up_Connection extends Activity {
    private Button onBtn;

    private TextView text;
    private BluetoothAdapter myBluetoothAdapter;
    protected Set<BluetoothDevice> pairedDevices;
    //sets the paired device object as a bluetooth device object container.
    protected ListView myListView;
    private ArrayAdapter<String> BTArrayAdapter;
//containers for variables, many of the variables relate to the buttons defined in the activity_my.xml.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.set_up_bt);
    //on the apps creation set the content view to the activity_my.xml layout.

        // take an instance of BluetoothAdapter - Bluetooth radio
        myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        //get the default adater string/id and place it in myBluetoothAdapter
        if(myBluetoothAdapter == null) {
            //if an adapter doesn't exist
            onBtn.setEnabled(false);
            text.setText("Status: not supported");
            Toast.makeText(getApplicationContext(), "Your device does not support Bluetooth",
                    Toast.LENGTH_LONG).show();
            //display a long notification that the device doesn't have bluetooth.
        } else {
            text = (TextView) findViewById(R.id.text);
            //make the text variable equal the type 'TextView' of the 'R.id.text' id value.
            //text =/= 'R.id.text but rather text = the address of 'R.id.text'
            //whatever 'R.id.text' equals, 'text' now equals.

            onBtn = (Button)findViewById(R.id.turnOn);
            //onBtn = type 'Button' address 'R.id.turnOn'
            onBtn.setOnClickListener(new View.OnClickListener() {
                //set an onclick listener to do something.

                @Override
                public void onClick(View v) {

                    on(v);
                }
                //when the 'turnon' button is pressed run the method 'on' and pass it the current view 'v'
            });


            myListView = (ListView)findViewById(R.id.listView1);

            // create the arrayAdapter that contains the BTDevices, and set it to the ListView
            BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
            //create an array adapter object of type String using 'this' context and the 'simple list item 1' layout.

            myListView.setAdapter(BTArrayAdapter);
            //set the ListView to the Array Adapter creating a list of the 'BTArrayAdapter' array
        }
    }

    public void on(View view) {

        if (!myBluetoothAdapter.isEnabled()) {

            //isEnabled checks if the bluetooth adapter is enabled.
            Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            //start an intent to show a system activity that allows the user to turn on Bluetooth.
        }
        else{
            Toast.makeText(getApplicationContext(),"Bluetooth is already on",
                    Toast.LENGTH_LONG).show();
            //else display a notification that BT is already on.
        }
    }
}





AndroidManifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.evenfurtherbeyond.bluetoothsendrecieve" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Set_Up_Connection"
            android:label="Set Up Bluetooth"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.evenfurtherbeyond.MainActivity"/>
        </activity>

    </application>

</manifest>


日志猫

    11-17 19:38:09.585    8472-8472/com.evenfurtherbeyond.bluetoothsendrecieve E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.SecurityException: Need BLUETOOTH_ADMIN permission: Neither user 10084 nor current process has android.permission.BLUETOOTH_ADMIN.
        at android.os.Parcel.readException(Parcel.java:1425)
        at android.os.Parcel.readException(Parcel.java:1379)
        at android.bluetooth.IBluetooth$Stub$Proxy.disable(IBluetooth.java:1048)
        at android.bluetooth.BluetoothAdapter.disable(BluetoothAdapter.java:527)
        at com.evenfurtherbeyond.bluetoothsendrecieve.Set_Up_Connection.off(Set_Up_Connection.java:211)
        at com.evenfurtherbeyond.bluetoothsendrecieve.Set_Up_Connection$2.onClick(Set_Up_Connection.java:88)
        at android.view.View.performClick(View.java:4209)
        at android.view.View$PerformClick.run(View.java:17431)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5297)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

最佳答案

有人请求日志猫后,我看到问题已记录。
使用以下命令更新清单以包括Bluetooth和Bluetooth_Admin权限后:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>


来自教程http://developer.android.com/guide/topics/security/permissions.html
该应用程序运行顺利。

09-26 12:38