Bluetooth Plugin by Scott H Tech

Hi there!

I try to connect and send message with the Bluetooth plugin. But, I have some problema to do that.

When I try to connect on the Notebook, raspberry or other phone, I have this message:

(https://drive.google.com/file/d/0B4XdlT46p6gLQXoyMW5QU2tzOGVsNFB3ZlVWbVVYU0g4Q2hn/view)

But, when I try to connect with my sound lamp (For testing) I has a sucessful.

LAMP LIKE THIS: (https://www.casasbahia-imagens.com.br/audio/CaixasAcusticas/11381888/977260153/lampada-led-rgb-com-caixa-de-som-e-bluetooth-11381888.jpg)

And I try to send message to it, and no have error.

I already try connect in the raspberry, after open my app and send message (bt.send) and I have this error:

Runtime Error: bluetoothView.lua:87: java.lang.NullPointerException: Attempt to invoke virtual method ‘void java.io.OutputStream.write(byte[])’ on a null object reference

Someone can help me?

Obs:  my progect is use the app like a client and raspberry like a server.

Here the code: 

----------------------------------------------------------------------------------------- -- --\> bluetoothView --\> Composer to waiting bluetooth connection -- ----------------------------------------------------------------------------------------- ---- Permission -- request premission for 6.0+ native.showPopup( "requestAppPermission", {appPermission = "Location", urgency = "Critical", listener= function ( e ) end}) -- Variable local bt = require "plugin.bt" local widget = require "widget" local scene = composer.newScene() local tDeviceList = {} local bConected = false -- Function function scene:create( event ) local sceneGroup = self.bluetoothView ------------- BACKGROUND ------------- local bg = display.newImage("img/background.png") bg:translate( nW\*0.5, nH\*0.8 ) display.setDefault("background", 0,0.3,0.8 ) -- Color ------------- LABEL ------------- local label = display.newText("Conecte ao \n Carro", nW\*0.5, 40, nil, 35 ) label:setFillColor( 0 ) ------------- TABLE ------------- local function onRowRender( event ) -- Row References local row = event.row local rowHeight = row.contentHeight local rowWidth = row.contentWidth ---- Column 01 local column1 = display.newText( row, tDeviceList[row.index][1], 0, 0, nil, 14 ) column1.y = rowHeight \* 0.5 column1.x = rowWidth \* 0.2 column1:setFillColor( 0 ) ---- Column 02 local column2 = display.newText( row, tDeviceList[row.index][2], 0, 0, nil, 8 ) column2.y = rowHeight \* 0.5 column2.x = rowWidth \* 0.8 column2:setFillColor( 0 ) end local function onRowTouch( event ) native.setActivityIndicator( true ) native.showAlert( "Conectado?", bConected, {"Ok"} ) if ( event.phase == 'tap') then if not bConected then bt.connect(tDeviceList[event.target.index][0]) else bt.disconnect(tDeviceList[event.target.index][0]) end end end local function existIDinTable( ID ) for i=1,#tDeviceList do if (tDeviceList[i][0] == ID ) then return true end end return false end local tableView = widget.newTableView({noLines=true, hideBackground=true, left=10, top=110, height=200, width=300,onRowRender=onRowRender,onRowTouch=onRowTouch}) ------------- BUTTON ------------- local function buttonTouch1() if bt.isEnabled() then tableView:deleteAllRows() tDeviceList = nil tDeviceList = {} bt.search() native.setActivityIndicator( true ) end end local button1 = widget.newButton({label="Procurar", labelAlign=center, labelColor={default={ 0, 0, 0 }, over={ 1, 1, 1 }}, onPress=buttonTouch1, left=nW\*0, top=nH\*0.6, width=200, height=40}) local function buttonTouch2() if bt.isEnabled() then bt.send("Test!\n") end end local button2 = widget.newButton({label="Enviar", labelAlign=center, labelColor={default={ 0, 0, 0 }, over={ 1, 1, 1 }}, onPress=buttonTouch2, left=nW\*0.4, top=nH\*0.6, width=200, height=40}) ------------- BLUETOOTH INIT ------------- bt.init(function (event) -- Device Found if (event.type == "device found") then if ( existIDinTable(event.deviceID)==false) then tableView:deleteAllRows() local x = #tDeviceList+1 tDeviceList[x] = {} tDeviceList[x][0] = event.deviceID tDeviceList[x][1] = event.deviceName tDeviceList[x][2] = event.deviceState for x = 1, #tDeviceList do tableView:insertRow{} end end end -- Discovery Finished if (event.type == "discovery finished") then native.setActivityIndicator( false ) end if (event.type == "connected") then native.showAlert( "Conectado!", "Dispositivo: "..event.deviceName, {"Ok"} ) bConected = true native.setActivityIndicator( false ) end if (event.type == "disconnect") then native.showAlert( "Desconectado!", "De: "..event.deviceName, {"Ok"} ) native.setActivityIndicator( false ) end if (event.type == "message") then native.showAlert( "Message Received", "Name: "..event.error, {"Ok"} ) native.setActivityIndicator( false ) end if (event.type == "connection error") then native.showAlert( "Connection error", "Name:"..event.deviceName.."/Error: "..event.error, {"Ok"} ) native.setActivityIndicator( false ) end if (event.type == "error") then native.showAlert( "Disconnected from device", "Name:"..event.error, {"Ok"} ) native.setActivityIndicator( false ) end end) ------------- TEST AREA ------------- end function scene:show( event ) end function scene:hide( event ) end function scene:destroy( event ) end -- EventListener scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene