无法将字节从活动发送到蓝牙接收器?

时间:2018-07-22 06:15:51

标签: android bluetooth android-bluetooth alarm

我正在尝试将android设备中“警报通知”活动中的某些字符发送到Bluetooth接收器。但是,当我尝试发送数据时,抛出异常,即我通过write()方法传递了一个空对象。在活动中初始化该对象时,我无法理解该对象为NULL。

当我尝试通过不同于“警报通知”的活动发送相同的字符时,效果很好。

“警报通知”活动中是否存在某些阻止此行为的代码?

请原谅我的业余编码技能,并指导我正确的方向。

谢谢。

AlarmNotification.java

package com.example.android.cheesesquare;

/**
 * Created by HARDIK SHARMA on 5/25/2018.
 */

import...

public class AlarmNotification extends Activity
{
private final String TAG = "AlarmMe";
private Ringtone mRingtone;
private Vibrator mVibrator;
private final long[] mVibratePattern = { 0, 500, 500 };
private boolean mVibrate;
private Uri mAlarmSound;
private long mPlayTime;
private Timer mTimer = null;
private Alarm mAlarm;
private DateTime mDateTime;
private TextView mTextView;
private PlayTimerTask mTimerTask;

BluetoothConnectionService mBluetoothConnection;

String transmit = "";

@Override
protected void onCreate(Bundle bundle)
{
    super.onCreate(bundle);

    getWindow().addFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    setContentView(R.layout.notification);

    mDateTime = new DateTime(this);
    mTextView = (TextView)findViewById(R.id.alarm_title_text);

    readPreferences();

    mRingtone = RingtoneManager.getRingtone(getApplicationContext(), mAlarmSound);
    if (mVibrate)
        mVibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);

    start(getIntent());

}

@Override
protected void onDestroy()
{
    super.onDestroy();
    Log.i(TAG, "AlarmNotification.onDestroy()");
    stop();
}

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    Log.i(TAG, "AlarmNotification.onNewIntent()");

    addNotification(mAlarm);

    stop();
    start(intent);
}

private void start(Intent intent)
{
    mAlarm = new Alarm(this);
    mAlarm.fromIntent(intent);

    Log.i(TAG, "AlarmNotification.start('" + mAlarm.getTitle() + "')");

    mTextView.setText(mAlarm.getTitle());

    transmit = mAlarm.getTitle();

    byte[] bytes = transmit.toUpperCase().getBytes(Charset.defaultCharset());
    mBluetoothConnection.write(bytes);

    mTimerTask = new PlayTimerTask();
    mTimer = new Timer();
    mTimer.schedule(mTimerTask, mPlayTime);
    mRingtone.play();
    if (mVibrate)
        mVibrator.vibrate(mVibratePattern, 0);
}

private void stop()
{
    Log.i(TAG, "AlarmNotification.stop()");

    mTimer.cancel();
    mRingtone.stop();
    if (mVibrate)
        mVibrator.cancel();
}

public void onDismissClick(View view)
{
    finish();
}

private void readPreferences()
{
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    mAlarmSound = Uri.parse(prefs.getString("alarm_sound_pref", "DEFAULT_RINGTONE_URI"));
    mVibrate = prefs.getBoolean("vibrate_pref", true);
    mPlayTime = (long)Integer.parseInt(prefs.getString("alarm_play_time_pref", "30")) * 1000;
}

private void addNotification(Alarm alarm)
{
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification;
    PendingIntent activity;
    Intent intent;

    Log.i(TAG, "AlarmNotification.addNotification(" + alarm.getId() + ", '" + alarm.getTitle() + "', '" + mDateTime.formatDetails(alarm) + "')");

    intent = new Intent(this.getApplicationContext(), AlarmMe.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    activity = PendingIntent.getActivity(this, (int)alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    notification = builder
            .setContentIntent(activity)
            .setSmallIcon(R.drawable.baseline_alarm_black_18dp)
            .setAutoCancel(true)
            .setContentTitle("Missed alarm: " + alarm.getTitle())
            .setContentText(mDateTime.formatDetails(alarm))
            .build();

    notificationManager.notify((int)alarm.getId(), notification);
}

@Override
public void onBackPressed()
{
    finish();
}

private class PlayTimerTask extends TimerTask
{
    @Override
    public void run()
    {
        Log.i(TAG, "AlarmNotification.PlayTimerTask.run()");
        addNotification(mAlarm);
        finish();
    }
}

我的Logcat:

11893-11893/com.example.android.cheesesquare E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.cheesesquare, PID: 11893
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.cheesesquare/com.example.android.cheesesquare.AlarmNotification}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.android.cheesesquare.BluetoothConnectionService.write(byte[])' on a null object reference
                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
                   at android.os.Handler.dispatchMessage(Handler.java:102)
                   at android.os.Looper.loop(Looper.java:154)
                   at android.app.ActivityThread.main(ActivityThread.java:6165)
                   at java.lang.reflect.Method.invoke(Native Method)
                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.android.cheesesquare.BluetoothConnectionService.write(byte[])' on a null object reference
                   at com.example.android.cheesesquare.AlarmNotification.start(AlarmNotification.java:224)
                   at com.example.android.cheesesquare.AlarmNotification.onCreate(AlarmNotification.java:89)
                   at android.app.Activity.performCreate(Activity.java:6687)
                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 
                   at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490) 
                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                   at android.os.Looper.loop(Looper.java:154) 
                   at android.app.ActivityThread.main(ActivityThread.java:6165) 
                   at java.lang.reflect.Method.invoke(Native Method) 
                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 

我已经来这几天了。请帮忙!!!

编辑 初始化BluetoothService后的Logcat。

FATAL EXCEPTION: main
                                                                                Process: com.example.android.cheesesquare, PID: 7482
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.cheesesquare/com.example.android.cheesesquare.AlarmNotification}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.android.cheesesquare.BluetoothConnectionService$ConnectedThread.write(byte[])' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
                                                                                    at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6165)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.android.cheesesquare.BluetoothConnectionService$ConnectedThread.write(byte[])' on a null object reference
                                                                                    at com.example.android.cheesesquare.BluetoothConnectionService.write(BluetoothConnectionService.java:306)
                                                                                    at com.example.android.cheesesquare.AlarmNotification.start(AlarmNotification.java:120)
                                                                                    at com.example.android.cheesesquare.AlarmNotification.onCreate(AlarmNotification.java:82)
                                                                                    at android.app.Activity.performCreate(Activity.java:6687)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 
                                                                                    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6165) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 

1 个答案:

答案 0 :(得分:0)

您没有通过write()方法传递空对象。您的mBluetoothConnection对象为null。 您没有在活动中初始化mBluetoothConnection