远程服务调用PeripheralManager.getInstance

时间:2018-05-30 11:03:33

标签: android remote-server android-things

运行时异常:

  

System.err:java.lang.RuntimeException:Stub!   在com.google.android.things.pio.PeripheralManager.getInstance(PeripheralManager.java:21)   在com.afollestad.remotedemo.RemoteService $ 1.testPeripheralManager(RemoteService.java:33)   在com.afollestad.remotedemo.IRemoteAIDL $ Stub.onTransact(IRemoteAIDL.java:56)   在android.os.Binder.execTransact(Binder.java:697)

远程服务的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.afollestad.remotedemo">
<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />


<application>
    <service
        android:name=".RemoteService"
        android:process=":remote">
        <intent-filter>
            <action android:name="service.remote" />
        </intent-filter>
    </service>
</application>

MainActivity:

public class MainActivity extends Activity {
private IRemoteAIDL mService;
private TextView result;
private Button connectRemote;
private Button disconnectRemote;
private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        Log.d("aidl", "onServiceConnected" + service + "  className  " + className);
        mService = IRemoteAIDL.Stub.asInterface(service);
        try {
            mService.testPeripheralManager();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void onServiceDisconnected(ComponentName className) {
        Log.d("aidl", "onServiceDisconnected");
        mService = null;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = (TextView) findViewById(R.id.result);
        connectRemote = (Button) findViewById(R.id.connect_remote);
        disconnectRemote = (Button) findViewById(R.id.disconnect_remote);
        connectRemote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("aidl", "onClick new thread");
                Intent intent = new Intent();
                intent.setAction("service.remote");
                intent.setPackage("com.afollestad.remotedemo");
                bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

            }
        });
        disconnectRemote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("aidl", "onClick");

                unbindService(mConnection);
            }
        });
}}

远程服务:

public class RemoteService extends Service {
private I2cDevice mI2cDevice = null;
private static final String I2C_DEVICE_NAME = "I2C1";
private static final int I2C_ADDRESS = 0x5c;

public Context mContext;
private boolean flag;

@Override
public void onCreate() {
    super.onCreate();
}

private final IRemoteAIDL.Stub remoteBinder = new IRemoteAIDL.Stub() {

    @Override
    public void testPeripheralManager(){
        Log.d("aidl", "RemoteService PeripheralManager");
        try {
            PeripheralManager manager = PeripheralManager.getInstance();
            Log.d("aidl", "PeripheralManager2");
            mI2cDevice = manager.openI2cDevice(I2C_DEVICE_NAME, I2C_ADDRESS);
            if (mI2cDevice != null) Log.i("aidl", "I2C device open successful!");
        }catch(RuntimeException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
};

@Override
public IBinder onBind(Intent intent) {
    Log.d("aidl", "onBind");
    mContext = this;
    return remoteBinder;
}

@Override
public boolean onUnbind(Intent intent) {
    flag = true;
    return super.onUnbind(intent);
}

我在android事件模块中建立远程服务,在远程服务中调用PeripheralManager.getInstance()。

如果有任何解决这个问题,请帮助我。

1 个答案:

答案 0 :(得分:1)

你应该有一个uses-library作为Manifest声明的一部分,这会阻止你在没有它的设备中安装需要Android Things的apk,这看起来就像这里发生的那样。