0

想用远程蓝牙服务器实现数据传输,于是开始探索androids蓝牙功能,一步步走,现在卡在一个程序里。将程序加载到模拟器后,它会响应“Adil_Bluetooth_1st 已停止”。

我为您提供我的文件:

XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/acceleration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="X: Y: Z:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ProgressBar
        android:id="@+id/progressBarX"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/acceleration"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/acceleration"
        android:layout_marginLeft="19dp"
        android:layout_marginRight="19dp"
        android:layout_marginTop="40dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/progressBarX"
        android:layout_alignTop="@+id/acceleration"
        android:text="               Noise Margin" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBarX"
        android:layout_below="@+id/progressBarX"
        android:text="X-axis" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBarZ"
        android:layout_below="@+id/progressBarY"
        android:text="Y-axis" />

    <ProgressBar
        android:id="@+id/progressBarY"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignRight="@+id/progressBarX"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="72dp" />

    <EditText
        android:id="@+id/eText"
        android:layout_width="50dp"
        android:layout_height="45dp"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignTop="@+id/textView1"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBarZ"
        android:layout_alignTop="@+id/progressBarZ"
        android:layout_marginTop="20dp"
        android:text="Z-axis" />

    <ProgressBar
        android:id="@+id/progressBarZ"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBarY"
        android:layout_alignRight="@+id/progressBarY"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="84dp" />

</RelativeLayout>

这是我的ActivityMain.java文件:

package com.example.adil_bluetooth_1st;

import java.util.Set;

import javax.xml.datatype.Duration;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.bluetooth.*;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class Bluetooth extends Activity {

    private BluetoothAdapter mBluetoothAdapter;
    Integer REQ_BT_ENABLE=1;
    private ArrayAdapter<String> mArrayAdapter;
    Set<BluetoothDevice> pairedDevices;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth);
        mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
        String Status;
        if(mBluetoothAdapter==null)
        {
            Toast.makeText(this, "Device doesn't support Bluetooth", Toast.LENGTH_LONG).show();

        }
        /*
         * checking whether Bluetooth is enabled ,if not enable it!
         * */
        if(mBluetoothAdapter.isEnabled())
        {
            String mydeviceadress=mBluetoothAdapter.getAddress();
            String mydevicename=mBluetoothAdapter.getName();
            Status="My Device Name is:"+mydevicename+"\nThe Adress is:"+mydeviceadress;
            Toast.makeText(this, Status, Toast.LENGTH_LONG).show();
        }
        else
        {
             Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
             startActivityForResult(enableBtIntent, REQ_BT_ENABLE);
             Toast.makeText(this, "Enabling Bluetooth!!", Toast.LENGTH_LONG).show();
        }
        /*
         * Getting paired devices and saving them on an ArrayAdapter
         * */
        pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
                // Add the name and address to an array adapter to show in a ListView
                mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }
        /*Create a BroadcastReceiver for ACTION_FOUND
         *
         * */


        final BroadcastReceiver Receiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                // When discovery finds a device
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    // Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    // Add the name and address to an array adapter to show in a ListView
                    mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
            }
        };
        // Register the BroadcastReceiver
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(Receiver, filter);


        /*
         * to make the device discoverable
         * */
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bluetooth, menu);
        return true;
    }

}

LogCat 消息:

> 04-04 16:41:58.051: E/BluetoothAdapter(1082): Bluetooth binder is null
> 04-04 16:41:58.111: D/AndroidRuntime(1082): Shutting down VM 04-04
> 16:41:58.111: W/dalvikvm(1082): threadid=1: thread exiting with
> uncaught exception (group=0x40a71930) 04-04 16:41:58.131:
> E/AndroidRuntime(1082): FATAL EXCEPTION: main 04-04 16:41:58.131:
> E/AndroidRuntime(1082): java.lang.RuntimeException: Unable to start
> activity
> 

ComponentInfo{com.example.adil_bluetooth_1st/com.example.adil_bluetooth_1st.Bluetooth}:
> java.lang.NullPointerException 04-04 16:41:58.131:
> E/AndroidRuntime(1082):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
> 04-04 16:41:58.131: E/AndroidRuntime(1082):   at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
> 04-04 16:41:58.131: E/AndroidRuntime(1082):   at
> android.app.ActivityThread.access$600(ActivityThread.java:141) 04-04
> 16:41:58.131: E/AndroidRuntime(1082):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
> 04-04 16:41:58.131: E/AndroidRuntime(1082):   at
> android.os.Handler.dispatchMessage(Handler.java:99) 04-04
> 16:41:58.131: E/AndroidRuntime(1082):     at
> android.os.Looper.loop(Looper.java:137) 04-04 16:41:58.131:
> E/AndroidRuntime(1082):   at
> android.app.ActivityThread.main(ActivityThread.java:5041) 04-04
> 16:41:58.131: E/AndroidRuntime(1082):     at
> java.lang.reflect.Method.invokeNative(Native Method) 04-04
> 16:41:58.131: E/AndroidRuntime(1082):     at
> java.lang.reflect.Method.invoke(Method.java:511) 04-04 16:41:58.131:
> E/AndroidRuntime(1082):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
> 04-04 16:41:58.131: E/AndroidRuntime(1082):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-04
> 16:41:58.131: E/AndroidRuntime(1082):     at
> dalvik.system.NativeStart.main(Native Method) 04-04 16:41:58.131:
> E/AndroidRuntime(1082): Caused by: java.lang.NullPointerException
> 04-04 16:41:58.131: E/AndroidRuntime(1082):   at
> com.example.adil_bluetooth_1st.Bluetooth.onCreate(Bluetooth.java:38)
> 04-04 16:41:58.131: E/AndroidRuntime(1082):   at
> android.app.Activity.performCreate(Activity.java:5104) 04-04
> 16:41:58.131: E/AndroidRuntime(1082):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
> 04-04 16:41:58.131: E/AndroidRuntime(1082):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
> 04-04 16:41:58.131: E/AndroidRuntime(1082):   ... 11 more
4

1 回答 1

1

Waqas在这里回答了类似的问题

您的 [Bluetooth] 服务可能不为空,因为绑定服务是一种异步方法

于 2013-04-04T16:59:15.810 回答