无法将字节从 activity 发送到蓝牙接收器?
Unable to send bytes from activity to Bluetooth receiver?
我正在尝试从 android 设备中的 "alarm notification" activity 向蓝牙接收器发送某些字符。但是当我尝试发送数据时抛出一个异常,我正在通过 write() 方法传递一个空对象。当我在 activity.
中初始化对象时,我无法理解该对象如何为 NULL
当我尝试从不同于 "alarm notification" 的 activity 中发送相同的字符时,它工作正常。
"alarm notification" activity 中是否有一些代码阻碍了这一点?
请原谅我的业余编码技能并指导我正确的方向。
谢谢。
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)
我已经在这里工作好几天了。请帮忙!!!
编辑
Logcat 初始化 BluetoothService 后。
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)
您没有通过 write() 方法传递空对象。您的 mBluetoothConnection
对象为空。
您没有在 activity
中初始化 mBluetoothConnection
我正在尝试从 android 设备中的 "alarm notification" activity 向蓝牙接收器发送某些字符。但是当我尝试发送数据时抛出一个异常,我正在通过 write() 方法传递一个空对象。当我在 activity.
中初始化对象时,我无法理解该对象如何为 NULL当我尝试从不同于 "alarm notification" 的 activity 中发送相同的字符时,它工作正常。
"alarm notification" activity 中是否有一些代码阻碍了这一点?
请原谅我的业余编码技能并指导我正确的方向。
谢谢。
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)
我已经在这里工作好几天了。请帮忙!!!
编辑 Logcat 初始化 BluetoothService 后。
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)
您没有通过 write() 方法传递空对象。您的 mBluetoothConnection
对象为空。
您没有在 activity
mBluetoothConnection