BluetoothLeGatt实施

时间:2016-09-12 08:37:14

标签: android bluetooth-lowenergy manifest

我正在尝试将android中的BluetoothLeGatt示例项目实施到我自己的应用程序中来处理BLE服务。

BluetoothLeGatt Example Android

实际状态是我有自己的应用程序,只有一个活动,我在其他页面之间切换片段。我想实现android示例,这样我就可以接收蓝牙数据并将其保存到自己的数据类中(通过接口或类似现在不确定的东西)。

如果我在设备上运行蓝牙示例,则无任何问题。我还可以将该示例实现到我的应用程序中,并将其用作“启动活动”,并使用以下清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bb.app">
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:debuggable="true">


        <activity
            android:name=".DeviceScanActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name">
            <!--android:theme="@style/AppTheme.NoActionBar"-->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name=".DeviceControlActivity"/>
        <activity android:name=".MyActivity"/>
        <service android:name=".BluetoothLeService" android:enabled="true"/>
        
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>
</manifest>

但是我无法在示例活动和我自己的活动之间切换。我正在使用带有onClickListener的简单按钮来切换它:

 buttonChangeActivity = (Button) findViewById(R.id.buttonChangeActivity);

        buttonChangeActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(DeviceControlActivity.this, "PRESSED", Toast.LENGTH_SHORT).show();

                try{
                    Intent myIntent = new Intent(DeviceControlActivity.this, MyActivity.class);
                    DeviceControlActivity.this.startActivity(myIntent);
                }catch (Exception e){
                    Toast.makeText(DeviceControlActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
                }

            }
        });

它仍然有效。但后来它说MyActivity类需要使用Theme.AppCompat,但我已经在使用它了。这是完整的错误代码:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bb.app/com.example.bb.app.MyActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5258)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                        at com.example.bb.app.MyActivity.onCreate(MyActivity.java:262)
                                                                        at android.app.Activity.performCreate(Activity.java:5990)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)

我在我的活动中使用了一个工具栏,示例Project使用了一个操作栏,所以我很确定我需要在我的项目运行时在主题之间“切换”,但我不知道如何操作。我已经阅读了一些有关此行为的其他案例:

Stackoverflow 1

有关如何在项目运行时切换主题或在Android Studio中将其更改的位置的任何建议吗?

1 个答案:

答案 0 :(得分:0)

好的只需要几分钟,我已经解决了自己的问题。

刚刚更改了清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bb.app">
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar"
        android:debuggable="true">


        <activity
            android:name=".MyActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name">
            <!--android:theme="@style/AppTheme.NoActionBar"-->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".DeviceControlActivity"
                  android:theme="@android:style/Theme.Holo.Light"/>
        <activity android:name=".DeviceScanActivity"
                  android:theme="@android:style/Theme.Holo.Light"/>
        <service android:name=".BluetoothLeService" android:enabled="true"/>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>
</manifest>

所以改变每个活动的主题,无论如何都要感谢。