Android Studio蓝牙连接应用程序崩溃

时间:2018-09-18 17:32:12

标签: java android android-studio bluetooth android-bluetooth

我刚刚在我的应用程序中添加了新代码,该代码应该通过蓝牙连接到arduino以对其进行控制...我对Java编程还是相当陌生。编译没有显示任何错误,因此我在设备上启动了它,但是当我点击一个设备以连接它时,它就会崩溃。在下面,您将找到我的活动和错误日志:

我希望有人能提供帮助:)

BT_Classic.java:

package com.car.bluetooth.bluetoothcar;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;



public class BT_Classic extends AppCompatActivity {

private Button pairedButton;
private Button discoveredButton;
private Button btonButton;
private Button btoffButton;
private ProgressDialog progress;
ListView listView;
BluetoothSocket bluetoothSocket;
BluetoothDevice bluetoothDevice;


private final static UUID uuid = UUID.fromString("fc5ffc49-00e3-4c8b-9cf1-6b72aad1001a");


private ArrayList<String> mDeviceList = new ArrayList<String>();

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();


//BLUETOOTH VERBINDUNG


private static final int REQUEST_ENABLED = 0;
private static final int REQUEST_DISCOVERABLE = 0;

    private class ConnectingThread extends Thread {

        public ConnectingThread(BluetoothDevice device) {


            BluetoothSocket temp = null;
            BluetoothDevice bluetoothDevice = device;

            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try {
                temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
            } catch (IOException e) {
                e.printStackTrace();
            }
            bluetoothSocket = temp;

        }


            public void run() {
        // Cancel any discovery as it will slow down the connection
        btAdapter.cancelDiscovery();

        try {
            // This will block until it succeeds in connecting to the device
            // through the bluetoothSocket or throws an exception
            bluetoothSocket.connect();
        } catch (IOException connectException) {
            connectException.printStackTrace();
            try {
                bluetoothSocket.close();
            } catch (IOException closeException) {
                closeException.printStackTrace();
            }
        }

        // Code to manage the connection in a separate thread
    /*
        manageBluetoothConnection(bluetoothSocket);
    */
    }

    // Cancel an open connection and terminate the thread
    public void cancel() {
        try {
            bluetoothSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}








@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bt__classic);


    pairedButton = (Button) findViewById(R.id.pairedButton);
    discoveredButton = (Button) findViewById(R.id.discoveredButton);
    btonButton = (Button) findViewById(R.id.btonButton);
    btoffButton = (Button) findViewById(R.id.btoffButton);

    listView = (ListView) findViewById(R.id.listView);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String  itemValue = (String) listView.getItemAtPosition(position);
            String MAC = itemValue;
            if (itemValue.length() >= 17) {
                MAC = itemValue.substring(itemValue.length() - 17);
            }
            BluetoothDevice bluetoothDevice = btAdapter.getRemoteDevice(MAC);
            // Initiate a connection request in a separate thread
            ConnectingThread t = new ConnectingThread(bluetoothDevice);
            t.start();
        }
    });

    //Pairing Button

    pairedButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();

            ArrayList<String> devices = new ArrayList<String>();

            for (BluetoothDevice bt : pairedDevices){
                devices.add(bt.getName());
                devices.add(bt.getAddress());

            }

            ArrayAdapter arrayAdapter = new ArrayAdapter(BT_Classic.this, android.R.layout.simple_list_item_1, devices);
            listView.setAdapter(arrayAdapter);
        }
    });








    discoveredButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(!btAdapter.isDiscovering()){
                Intent bton = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                startActivityForResult(bton, REQUEST_DISCOVERABLE);
            }


        }
    });

    btonButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent bton = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(bton, REQUEST_ENABLED);
        }
    });

    btoffButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btAdapter.disable();
        }
    });










}
}

错误Logcat:

2018-09-18 18:22:00.692 619-739/? E/ANDR-PERF-MPCTL: Invalid profile no. 0,total profiles 0 only
2018-09-18 18:22:00.872 619-739/? E/ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only
2018-09-18 18:22:00.875 1633-1633/com.car.bluetooth.bluetoothcar E/InputEventReceiver: Exception dispatching input event.
2018-09-18 18:22:00.875 1633-1633/com.car.bluetooth.bluetoothcar E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2018-09-18 18:22:00.883 1633-1633/com.car.bluetooth.bluetoothcar E/MessageQueue- 
   JNI: java.lang.StringIndexOutOfBoundsException: length=5; index=-12
    at java.lang.String.substring(String.java:1935)
    at com.car.bluetooth.bluetoothcar.BT_Classic$1.onItemClick(BT_Classic.java:126)
    at android.widget.AdapterView.performItemClick(AdapterView.java:318)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1165)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3160)
    at android.widget.AbsListView.onTouchUp(AbsListView.java:4087)
    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3846)
    at android.view.View.dispatchTouchEvent(View.java:11724)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2958)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2636)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:445)
    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1828)
    at android.app.Activity.dispatchTouchEvent(Activity.java:3297)
    at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
    at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:407)
    at android.view.View.dispatchPointerEvent(View.java:11963)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4810)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4624)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4162)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4215)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4181)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4308)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4189)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4365)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4162)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4215)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4181)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4189)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4162)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6736)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6675)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6636)
    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6839)
    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:192)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:325)
    at android.os.Looper.loop(Looper.java:142)
    at android.app.ActivityThread.main(ActivityThread.java:6592)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
2018-09-18 18:22:00.891 1633-1633/com.car.bluetooth.bluetoothcar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.car.bluetooth.bluetoothcar, PID: 1633
java.lang.StringIndexOutOfBoundsException: length=5; index=-12
    at java.lang.String.substring(String.java:1935)
    at com.car.bluetooth.bluetoothcar.BT_Classic$1.onItemClick(BT_Classic.java:126)
    at android.widget.AdapterView.performItemClick(AdapterView.java:318)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1165)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3160)
    at android.widget.AbsListView.onTouchUp(AbsListView.java:4087)
    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3846)
    at android.view.View.dispatchTouchEvent(View.java:11724)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2958)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2636)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:445)
    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1828)
    at android.app.Activity.dispatchTouchEvent(Activity.java:3297)
    at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
    at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:407)
    at android.view.View.dispatchPointerEvent(View.java:11963)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4810)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4624)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4162)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4215)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4181)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4308)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4189)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4365)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4162)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4215)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4181)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4189)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4162)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6736)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6675)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6636)
    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6839)
    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:192)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:325)
    at android.os.Looper.loop(Looper.java:142)
    at android.app.ActivityThread.main(ActivityThread.java:6592)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)

第二个错误:

2018-09-18 21:00:03.412 3376-9580/? E/CastSocket: [MiniDeviceController-1] Failed to shutdown the output stream socket: nkr@19b00db.
java.net.SocketException: Socket is not connected
    at sun.nio.ch.Net.translateToSocketException(Net.java:129)
    at sun.nio.ch.Net.translateException(Net.java:166)
    at sun.nio.ch.Net.translateException(Net.java:172)
    at sun.nio.ch.SocketAdaptor.shutdownOutput(SocketAdaptor.java:424)
    at njq.c(:com.google.android.gms@13280018@13.2.80 (040308-211705629):16)
    at njz.run(:com.google.android.gms@13280018@13.2.80 (040308-211705629):18)
    at java.lang.Thread.run(Thread.java:764)
 Caused by: java.nio.channels.NotYetConnectedException
    at sun.nio.ch.SocketChannelImpl.shutdownOutput(SocketChannelImpl.java:842)
    at sun.nio.ch.SocketAdaptor.shutdownOutput(SocketAdaptor.java:422)
    at njq.c(:com.google.android.gms@13280018@13.2.80 (040308-211705629):16) 
    at njz.run(:com.google.android.gms@13280018@13.2.80 (040308-211705629):18) 
    at java.lang.Thread.run(Thread.java:764) 

1 个答案:

答案 0 :(得分:0)

下面的import itertools from functools import reduce def change(target): nums = [5, 7] min_len = int(target / 7) max_len = int(target / 5) + 1 res = reduce(lambda x, y: x + (list(itertools.combinations_with_replacement(nums, y))), range(min_len, max_len), []) return list(filter(lambda x: sum(x) == target, res)) 函数中的特定行String MAC = itemValue.substring(itemValue.length() - 17);导致onItemClick()

java.lang.StringIndexOutOfBoundsException

要解决此问题,您可能需要确保原始listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String itemValue = (String) listView.getItemAtPosition(position); String MAC = itemValue.substring(itemValue.length() - 17); BluetoothDevice bluetoothDevice = btAdapter.getRemoteDevice(MAC); // Initiate a connection request in a separate thread ConnectingThread t = new ConnectingThread(bluetoothDevice); t.start(); } }); String)的长度大于您要提取的子字符串的长度-

itemValue