connectAsync() crashing app

Greetings

I am trying to develop an Android app for my MetaWear C board that makes use of the Sensor Fusion data.
Reading the documentation, it says that before I can make use of any of the sensors, I need to make a call to connectAsync() , nevertheless, the app crashes right after the method is called, and I still don't understand why.

This is what I get on Logcat on Android Studio:
2019-01-07 21:46:11.166 13465-16502/? E/AudioProvider: AudioRecord is null, can't start recording
2019-01-07 21:46:11.186 13465-13465/? E/HotwordDetector: Invalid or incompatible speaker models. Silent enrollment required.
java.lang.IllegalArgumentException: Invalid speaker model provided
at com.google.speech.micro.GoogleHotwordRecognizer.nativeNew(Native Method)
at com.google.speech.micro.GoogleHotwordRecognizer.(SourceFile:6)
at com.google.android.libraries.assistant.hotword.k.a(SourceFile:66)
at com.google.android.libraries.assistant.hotword.k.at(SourceFile:35)
at com.google.android.apps.gsa.voiceinteraction.hotword.a.dIB(SourceFile:48)
at com.google.android.apps.gsa.voiceinteraction.hotword.b.handleMessage(SourceFile:4)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6753)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

This is the all the code I have so far on my MainActivity, which is the only Activity I have on this project:
package com.example.pedro.metaweartest3;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import bolts.Continuation;
import bolts.Task;

import com.mbientlab.metawear.MetaWearBoard;
import com.mbientlab.metawear.android.BtleService;

public class MainActivity extends AppCompatActivity implements ServiceConnection {
private MetaWearBoard board;
private BtleService.LocalBinder localBinder;
private final String MAC_ADDRESS = "CB:42:F6:6E:2C:7A";

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


    getApplicationContext().bindService(new Intent(this, BtleService.class),
            this, Context.BIND_AUTO_CREATE);
}

@Override
public void onDestroy() {
super.onDestroy();

    // Unbind the service when the activity is destroyed
    getApplicationContext().unbindService(this);
}




@Override
public void onServiceConnected(ComponentName name, IBinder binder){
    localBinder = (BtleService.LocalBinder) binder;
    retrieveBoard();
}
@Override
public void onServiceDisconnected(ComponentName name){

}


public void retrieveBoard() {
    final BluetoothManager btManager=
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    final BluetoothDevice remoteDevice=
            btManager.getAdapter().getRemoteDevice(MAC_ADDRESS);

    // Create a MetaWear board object for the Bluetooth Device
    board= localBinder.getMetaWearBoard(remoteDevice);
}




public void onClickTry(View view){
 board.connectAsync().continueWith(new Continuation<Void, Void>() {
     @Override
     public Void then(Task<Void> task) throws Exception {
         if (task.isFaulted()) {
             Log.i("MainActivity", "Failed to connect");
         } else {
             Log.i("MainActivity", "Connected");
         }
         return null;
     }
 });

}

}

I still cannot figure what could be causing the problem. Your help will be greatly appreciated. Thank you

Comments

  • That error doesn't appear relevant to any MetaWear code.

    Use the debugger to check that your variables aren't null and make sure you have declared the MetaWear service in your manifest file.

  • This is my AndroidManifest.xml :

    <?xml version="1.0" encoding="utf-8"?>




    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW"/>
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.mbientlab.metawear.android.BtleService" />
    </application>
    

    Also, I have been using the debugger to check my variables, and at the moment I invoke the onClickTry() method by pressing a button, the board variable has a value of JseMetaWearBoard@5583

    public void onClickTry(View view){
    board.connectAsync().continueWith(new Continuation<Void, Void>() { // board: JseMetaWearBoard@5583
    @Override
    public Void then(Task task) throws Exception {
    if (task.isFaulted()) {
    Log.i("MainActivity", "Failed to connect");
    } else {
    Log.i("MainActivity", "Connected");
    }
    return null;
    }
    });
    }

    If I let the program continue running with the next line, which is where connectAsync() is called, the program crashes and the only error I get on the Logcat is what I posted earlier.

    What else can I try to make this work?

  • edited January 9

    Wrap code blocks with ```

    Step though the conectAsync method with the debugger. There isn't much that can be done without knowing where the crash occurs.

    You can also try uisng the starter app template from the tutorial project.
    https://github.com/mbientlab/MetaWear-Tutorial-Android

  • public class MainActivity extends AppCompatActivity implements ServiceConnection {
    private MetaWearBoard board;

    private BtleService.LocalBinder localBinder;
    private final String MAC_ADDRESS = "CB:42:F6:6E:2C:7A";
    private  BluetoothManager  btManager;
    private  BluetoothDevice remoteDevice;
    private BluetoothLeScanner scanner;
    private TextView connectText;
    private TextView successText;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btManager= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        remoteDevice= btManager.getAdapter().getRemoteDevice(MAC_ADDRESS);
        scanner = btManager.getAdapter().getBluetoothLeScanner();
        connectText = findViewById(R.id.connect_text);
        successText = findViewById(R.id.board_connected_text);
    
        getApplicationContext().bindService(new Intent(this, BtleService.class),
                this, Context.BIND_AUTO_CREATE);
    }
    

    @Override
    public void onDestroy() {
    super.onDestroy();
    // Unbind the service when the activity is destroyed
    getApplicationContext().unbindService(this);
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder binder){
        localBinder = (BtleService.LocalBinder) binder;
    
    }
    @Override
    public void onServiceDisconnected(ComponentName name){
    
    }
    
    
    
    
    
    
    public void onClickTry(View view){
     board.connectAsync().continueWith(new Continuation<Void, Void>() {
         @Override
         public Void then(Task<Void> task) throws Exception {
             if (task.isFaulted()) {
                 runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         connectText.setText("Board is connected and ready to be used");
                     }
                 });
             } else {
                 runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         connectText.setText("Board is not ready to be used");
                     }
                 });
             }
             return null;
         }
     });
    

    }

    public void onClickConnect(View view){
    startScan();
    }

    ScanCallback callback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, final ScanResult result) {
    super.onScanResult(callbackType, result);

         runOnUiThread(new Runnable() {
             @Override
             public void run() {
                 remoteDevice =  result.getDevice();
                 board = localBinder.getMetaWearBoard(remoteDevice);
                 connectText.setText("The board was successfully found!");
    
             }
         });
    
    
    
     }
    

    };
    public void startScan(){
    List filters = new ArrayList<>();
    ScanFilter filter = new ScanFilter.Builder()
    .setDeviceAddress(MAC_ADDRESS)
    .build();

    filters.add(filter);
    
    ScanSettings settings = new ScanSettings.Builder().build();
    
    scanner.startScan(filters,settings,callback);
    

    }

    error:

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.pedro.metaweartest3, PID: 20419
    java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    Caused by: java.lang.BootstrapMethodError: Exception from call site #1 bootstrap method
    at com.mbientlab.metawear.android.BtleService$AndroidPlatform.connectAsync(BtleService.java:475)
    at com.mbientlab.metawear.impl.JseMetaWearBoard.connectAsync(JseMetaWearBoard.java:752)
    at com.example.pedro.metaweartest3.MainActivity.onClickTry(MainActivity.java:88)
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    Caused by: java.lang.ClassCastException: Bootstrap method returned null
    at com.mbientlab.metawear.android.BtleService$AndroidPlatform.connectAsync(BtleService.java:475)
    at com.mbientlab.metawear.impl.JseMetaWearBoard.connectAsync(JseMetaWearBoard.java:752)
    at com.example.pedro.metaweartest3.MainActivity.onClickTry(MainActivity.java:88)
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

  • This is how I modified my code so it makes a Bluetooth scan to retrieve the board instead of passing it the mac address to create the object.

    The second part is the exception I get in the debugger.

    Why is this happening? Thank you

  • Quoting myself

    @Eric said:
    Wrap code blocks with ```

  • @Eric said:
    Quoting myself

    @Eric said:
    Wrap code blocks with ```

    I'm sorry but I don't understand, what do you mean to use ```?

  • Exactly what it says. Wrap your code with 3 backticks, otherwise it will not render correctly on the page.

  • My app doesn't connect to Meta Motion Sensor R+. It keeps going to Update Firmware Screen. Tries to upload new firmware and then fails to connect. Tried all the steps listed numerous times with same results. Using Android phone. Last connection was this morning.

  • @PrakashDesai said:
    My app doesn't connect to Meta Motion Sensor R+. It keeps going to Update Firmware Screen. Tries to upload new firmware and then fails to connect. Tried all the steps listed numerous times with same results.

    Post unrelated issues in their own thread. Do not hijack existing threads.

  • public class MainActivity extends AppCompatActivity implements ServiceConnection {
    private MetaWearBoard board;
    
    private BtleService.LocalBinder localBinder;
    private final String MAC_ADDRESS = "CB:42:F6:6E:2C:7A";
    private  BluetoothManager  btManager;
    private  BluetoothDevice remoteDevice;
    private BluetoothLeScanner scanner;
    private TextView connectText;
    private TextView successText;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btManager= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        remoteDevice= btManager.getAdapter().getRemoteDevice(MAC_ADDRESS);
        scanner = btManager.getAdapter().getBluetoothLeScanner();
        connectText = findViewById(R.id.connect_text);
        successText = findViewById(R.id.board_connected_text);
    
        getApplicationContext().bindService(new Intent(this, BtleService.class),
                this, Context.BIND_AUTO_CREATE);
    }
    @Override
    public void onDestroy() {
    super.onDestroy();
    // Unbind the service when the activity is destroyed
    getApplicationContext().unbindService(this);
    }
    
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder){
        localBinder = (BtleService.LocalBinder) binder;
    
    }
    @Override
    public void onServiceDisconnected(ComponentName name){
    
    }
    
    
    
    
    
    
    public void onClickTry(View view){
     board.connectAsync().continueWith(new Continuation<Void, Void>() {
         @Override
         public Void then(Task<Void> task) throws Exception {
             if (task.isFaulted()) {
                 runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         connectText.setText("Board is connected and ready to be used");
                     }
                 });
             } else {
                 runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         connectText.setText("Board is not ready to be used");
                     }
                 });
             }
             return null;
         }
     });
    }
    
    public void onClickConnect(View view){
    startScan();
    }
    
    ScanCallback callback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, final ScanResult result) {
    super.onScanResult(callbackType, result);
    
         runOnUiThread(new Runnable() {
             @Override
             public void run() {
                 remoteDevice =  result.getDevice();
                 board = localBinder.getMetaWearBoard(remoteDevice);
                 connectText.setText("The board was successfully found!");
    
             }
         });
    
    
    
     }
    };
    public void startScan(){
    List filters = new ArrayList<>();
    ScanFilter filter = new ScanFilter.Builder()
    .setDeviceAddress(MAC_ADDRESS)
    .build();
    
    filters.add(filter);
    
    ScanSettings settings = new ScanSettings.Builder().build();
    
    scanner.startScan(filters,settings,callback);
    }
    
    
  • error:

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.pedro.metaweartest3, PID: 20419
    java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    Caused by: java.lang.BootstrapMethodError: Exception from call site #1 bootstrap method
    at com.mbientlab.metawear.android.BtleService$AndroidPlatform.connectAsync(BtleService.java:475)
    at com.mbientlab.metawear.impl.JseMetaWearBoard.connectAsync(JseMetaWearBoard.java:752)
    at com.example.pedro.metaweartest3.MainActivity.onClickTry(MainActivity.java:88)
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    Caused by: java.lang.ClassCastException: Bootstrap method returned null
    at com.mbientlab.metawear.android.BtleService$AndroidPlatform.connectAsync(BtleService.java:475)
    at com.mbientlab.metawear.impl.JseMetaWearBoard.connectAsync(JseMetaWearBoard.java:752)
    at com.example.pedro.metaweartest3.MainActivity.onClickTry(MainActivity.java:88)
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

  • That is my Android app code and the error I get when I try to call onClickTry()
    Any updates on this?
    Thank you

  • Step through the connectAsync function until you get to this line in the stack trace:

    com.mbientlab.metawear.android.BtleService$AndroidPlatform.connectAsync(BtleService.java:475)

    What is the state of the variables at that point?

    I've never seen this exception message with the API so not sure what is wrong:

    Caused by: java.lang.ClassCastException: Bootstrap method returned null

    Quoting myself again:

    @Eric said:
    You can also try uisng the starter app template from the tutorial project.
    https://github.com/mbientlab/MetaWear-Tutorial-Android

Sign In or Register to comment.