質問

MANIFESTファイルに許可を追加してデバイスをペアリングしましたが、ここでクラッシュしています。PaileDDevices= btadapter.getBondedDevices()を設定しています。

ボタン]クリックを介して接続しようとします:

private OnClickListener myListener = new OnClickListener() {
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.main_btnYes:
            connectToNXT(); // connect to NXT
                myIntent = new Intent(v.getContext(), SelectSession.class);
                startActivityForResult(myIntent, 0);
            break;
        case R.id.main_btnNo:
            myIntent = new Intent(v.getContext(), ExitScreen.class);
            startActivityForResult(myIntent, 0);
            break;
        }
    }
};

ConnectTonxt()方法は次のとおりです。クラッシュはここで発生します。 set bundeddevices = btadapter.getBondedDevices();private void connecttonxt(){

        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

        **Set<BluetoothDevice> bondedDevices = btAdapter.getBondedDevices();**

        BluetoothDevice nxtDevice = null;   

}

なぜこれがクラッシュを引き起こすのか知っていますか?

また、私はAndroidとBluetooth(2日間:D)をかなり慣れていないので、誰かがAndroid Bluetoothの良いチュートリアルを教えてくれますか?

ありがとう、

リッチ。

役に立ちましたか?

解決 3

まあ、さまざまなコードを試してみた後(どれもうまくいきませんでした...)、私はそれぞれからビットを取り、それを私のNXTで動作させることができました。

ファームウェア2.2.1でSamsung Galaxy Ace(Android OS)スマートフォンを使用しています

これが機能する接続方法です。必要に応じて自由に使用してください。

宣言:

    // This is the NXT Mac Address. Each device has a specific Mac. Find it in the Build output when uploading
    // your NXT app to the brick using a USB cable. MUST USE USB CABLE TO SEE MAC ADDRESS!
    final String nxtMac = "00:16:53:05:3C:F5";
    //Important: This is the data stream used to communicate with the NXT.
    private DataOutputStream nxtDos = null;
    BluetoothAdapter localAdapter;
    BluetoothSocket nxtSocket;
    boolean success = false;

接続メソッド

    //Connect to NXT
    public boolean connectToNXT() {         
        // get the BluetoothDevice of the NXT
        BluetoothDevice nxt = localAdapter.getRemoteDevice(nxtMac);
        //Try to connect to the nxt
        try {
            nxtSocket = nxt.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            nxtSocket.connect();
            //Get the Data stream
            nxtDos = new DataOutputStream(nxtSocket.getOutputStream());
            success = true;
        } catch (IOException e) {
            Log.d("Bluetooth", "Err: Device not found or cannot connect");
            success = false;
        }
        return success;
    }

必要に応じて、richardcloete@googlemail.comにメールしてください。

リッチ。

他のヒント

NXTのMacアドレスは、設定メニュー /NXTバージョンの下にあります。このオプションでは、ID番号はMACアドレスです。USBの必要はありません!

私の推測:nullpointerexception。 BTADAPTER変数はnullであり、そこからメソッドを呼び出しようとします。これにより、nullpointerexceptionが発生します。

しかし、スタックトレースか何かを提供できませんか?ログがなければ、何が起こったのかを知るのは難しいです。 Eclipseを使用する場合は、Window/Show/Android/LogCatに移動します。

アプリケーションをデバッグモードで実行し、アプリがクラッシュするラインの直前にブレークポイントを配置して、Btadapterに値があるかどうかを確認することもできます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top