代码之家  ›  专栏  ›  技术社区  ›  Hardik Sharma

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

  •  0
  • Hardik Sharma  · 技术社区  · 6 年前

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

    当我尝试从不同于“警报通知”的活动中发送相同的字符时,它工作正常。

    “报警通知”活动中是否有一些代码阻碍了这一点?

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

    谢谢您。

    报警通知.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();
        }
    }
    

    我的猫:

    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) 
    

    我已经做了好几天了。请帮忙!!!

    编辑 初始化Bluetooth服务后的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 回复  |  直到 6 年前
        1
  •  0
  •   Vivek Singh    6 年前

    不能通过write()方法传递空对象。你的 mBluetoothConnection 对象为空。 你没有草签 MBluetooth连接 在你的活动中